Hi,
I have a complex regular expression:
[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!
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
On 7 Oct 2007, at 09:38, John L. wrote:
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
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 seems to do the trick.
Fred
On 10/7/07, John L. [email protected] wrote:
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-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/
Ok thanks for the two suggestions. I never knew about %q.