# Assign a complex regular expression string

**URL:** <https://www.ruby-forum.com/t/assign-a-complex-regular-expression-string/116560>\
**Category:** Rails\
**Created:** [October 7, 2007, 10:38am UTC](https://www.ruby-forum.com/t/assign-a-complex-regular-expression-string/116560 "2007-10-07T10:38:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [October 7, 2007, 10:38am UTC](https://www.ruby-forum.com/t/assign-a-complex-regular-expression-string/116560/1 "2007-10-07T10:38:02Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [October 7, 2007, 2:07pm UTC](https://www.ruby-forum.com/t/assign-a-complex-regular-expression-string/116560/2 "2007-10-07T14:07:21Z")

</div>

> 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](http://www.ruby-doc.org/core/classes/Regexp.html#M001219)

Cheers  
Luke

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [October 7, 2007, 5:27pm UTC](https://www.ruby-forum.com/t/assign-a-complex-regular-expression-string/116560/3 "2007-10-07T17:27:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [October 7, 2007, 9:35pm UTC](https://www.ruby-forum.com/t/assign-a-complex-regular-expression-string/116560/4 "2007-10-07T21:35:07Z")

</div>

On 10/7/07, John L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [October 7, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/assign-a-complex-regular-expression-string/116560/5 "2007-10-07T23:16:45Z")

</div>

Ok thanks for the two suggestions. I never knew about %q.
