Ruby on Rails Monday, December 27, 2010

(Ruby 1.9.2) I have a simple validation regex which I need to pass the following values: "Billy-Bob" and "O'Kelley" (as test cases). Originally I was not allowing apostrophe but it became apparent I had to allow it.

The initial regex was:

/^[a-zA-Z -]*$/

Now, when I added the apostrophe like this:

/^[a-zA-Z' -']*$/

Then for some reason "Billy-Bob" was not getting matched:

> "Billy-Bob" =~ /^[a-zA-Z -']*$/
> nil
> "O'Kelley" =~ /^[a-zA-Z' -']*$/
> 0

But when I moved the apostrophe further in, then things work as desired an expected:

> "Billy-Bob" =~ /^[a-zA-Z' -]*$/
> 0
> "O'Kelley" =~ /^[a-zA-Z' -]*$/
> 0

Why is this?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment