Hi, I have a complex regular expression: [a-z0-9!#$%&''*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&''*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|biz|info|name|aero|biz|info|jobs|museum)\b And I want to assign it to a Constant which I''ll use to reference it in my code. If I put it between "" it gets evaluated, which breaks because it is full of "meaningful" characters. If I put it between '''' it breaks because it contains '' I was reluctant to fill the regexp with escape "/" characters as I am sure I will break it. Is there any other way to get this value assigned to a Constant ? Thanks! -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> I was reluctant to fill the regexp with escape "/" characters as I am > sure I will break it. >That should work unless maybe there''s something odd in your expression? In IRB if you try MYCONST = /foo/ => /foo/ "barfoo".index(MYCONST) => 3 If you''re sure your expression is correct maybe you could explicitly create a RegExp object? i.e. RegExp.new("regexp" ... See here: http://www.ruby-doc.org/core/classes/Regexp.html#M001219 Cheers Luke -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 7 Oct 2007, at 09:38, John Lane wrote:> > Hi, > > I have a complex regular expression: > > [a-z0-9!#$%&''*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&''*+/=?^_`{|}~-]+)*@(?: > [a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|biz| > info|name|aero|biz|info|jobs|museum)\b > > And I want to assign it to a Constant which I''ll use to reference > it in > my code. > > If I put it between "" it gets evaluated, which breaks because it is > full of "meaningful" characters. > If I put it between '''' it breaks because it contains ''Don''t forget there are other ways to define strings, in particular, %q<some string> You can use any character after the q (eg %q{some string}), although in general the point is to pick one which doesn''t occur much in the string itself, so that you don''t have to escape it. %q{} is like '''' in that it doesn''t try and interpolate stuff, whereas %Q{} is like "" in that it does. In your case %q<some string> seems to do the trick. Fred --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 10/7/07, John Lane <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi, > > I have a complex regular expression: > > [a-z0-9!#$%&''*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&''*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|biz|info|name|aero|biz|info|jobs|museum)\b > > And I want to assign it to a Constant which I''ll use to reference it in > my code. > > If I put it between "" it gets evaluated, which breaks because it is > full of "meaningful" characters. > If I put it between '''' it breaks because it contains '' > > I was reluctant to fill the regexp with escape "/" characters as I am > sure I will break it. > > Is there any other way to get this value assigned to a Constant ?Why not MyConstant = /[a-z0-9!#$%&''*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&''*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|biz|info|name|aero|biz|info|jobs|museum)\b/ -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ok thanks for the two suggestions. I never knew about %q. -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---