David Kahn
2010-Dec-27 16:42 UTC
Is apostrophe ('') something special in a regex if at end?
(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'' -'']*$/ > 0But 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'' -]*$/ > 0Why 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-Dec-27 16:52 UTC
Re: Is apostrophe ('') something special in a regex if at end?
On 27 December 2010 16:42, David Kahn <dk-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote:> (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?It is the minus that is a special char (as in a-z) if you escape the minus it is ok. ruby-1.9.2-p0 > "Billy-Bob" =~ /^[a-zA-Z \-'']*$/ => 0 ruby-1.9.2-p0 > "O''Kelley" =~ /^[a-zA-Z \-'']*$/ => 0 Colin> > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
David Kahn
2010-Dec-27 16:59 UTC
Re: Is apostrophe ('') something special in a regex if at end?
On Mon, Dec 27, 2010 at 10:52 AM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 27 December 2010 16:42, David Kahn <dk-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote: > > (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? > > It is the minus that is a special char (as in a-z) if you escape the > minus it is ok. >Yes I see you are right ---the weird part is that the minus is getting passed not as a special character in the first examples ( "Billy-Bob" =~ /^[a-zA-Z'' -]*$/ returns 0!) .... anyhow, I will remember than and start escaping it.> ruby-1.9.2-p0 > "Billy-Bob" =~ /^[a-zA-Z \-'']*$/ > => 0 > ruby-1.9.2-p0 > "O''Kelley" =~ /^[a-zA-Z \-'']*$/ > => 0 > > Colin > > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > > For more options, visit this group at > > http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-Dec-27 17:05 UTC
Re: Is apostrophe ('') something special in a regex if at end?
On 27 December 2010 16:59, David Kahn <dk-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote:>> ... >> It is the minus that is a special char (as in a-z) if you escape the >> minus it is ok. > > Yes I see you are right ---the weird part is that the minus is getting > passed not as a special character in the first examples ( "Billy-Bob" =~ > /^[a-zA-Z'' -]*$/ returns 0!) .... anyhow, I will remember than and start > escaping it.Yes I wondered about that. Either it is a bug or a documented feature that - does not need to be escaped in some circumstances. Or perhaps [a-zA-Z -] means something that neither of us understands. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rob Biedenharn
2010-Dec-27 17:07 UTC
Re: Is apostrophe ('') something special in a regex if at end?
On Dec 27, 2010, at 11:59 AM, David Kahn wrote:> It is the minus that is a special char (as in a-z) if you escape the > minus it is ok. > > Yes I see you are right ---the weird part is that the minus is > getting passed not as a special character in the first examples > ( "Billy-Bob" =~ /^[a-zA-Z'' -]*$/ returns 0!) .... anyhow, I will > remember than and start escaping it.The minus (hyphen) in a charset is un-special if it is at the beginning or the end. You''re better off escaping it yourself for exactly the reason you encountered – adding another character to the end changed the meaning of the regular expression (charset) in a way you didn''t expect. -Rob Rob Biedenharn Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org http://AgileConsultingLLC.com/ rab-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org http://GaslightSoftware.com/ -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
David Kahn
2010-Dec-27 17:11 UTC
Re: Is apostrophe ('') something special in a regex if at end?
On Mon, Dec 27, 2010 at 11:07 AM, Rob Biedenharn <Rob-GBZH0y1GwQfnZcttdmLDtYVOj8xgHMYO@public.gmane.orgm> wrote:> > On Dec 27, 2010, at 11:59 AM, David Kahn wrote: > > It is the minus that is a special char (as in a-z) if you escape the >> minus it is ok. >> > > Yes I see you are right ---the weird part is that the minus is getting > passed not as a special character in the first examples ( "Billy-Bob" =~ > /^[a-zA-Z'' -]*$/ returns 0!) .... anyhow, I will remember than and start > escaping it. > > > The minus (hyphen) in a charset is un-special if it is at the beginning or > the end. You''re better off escaping it yourself for exactly the reason you > encountered – adding another character to the end changed the meaning of the > regular expression (charset) in a way you didn''t expect. >Makes sense, thanks> > -Rob > > Rob Biedenharn > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org http://AgileConsultingLLC.com/ > rab-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org <Rob-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org> > http://GaslightSoftware.com/ > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.