# How to include a string into a regex comparison?

**URL:** <https://www.ruby-forum.com/t/how-to-include-a-string-into-a-regex-comparison/173016>\
**Category:** Ruby\
**Created:** [July 31, 2009, 11:26am UTC](https://www.ruby-forum.com/t/how-to-include-a-string-into-a-regex-comparison/173016 "2009-07-31T11:26:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [July 31, 2009, 11:26am UTC](https://www.ruby-forum.com/t/how-to-include-a-string-into-a-regex-comparison/173016/1 "2009-07-31T11:26:50Z")

</div>

Hi, I don’t get solving the following issue (sure it’s easy but I cannot  
do it):

valid\_content\_type = “application/auth-policy+xml”  
received\_content\_type = “application/auth-policy+xml;charset=ISO-8859-1”

received\_content\_type =~ /^application/auth-policy+xml;?/  
=\> 0

received\_content\_type =~ /^#{received\_content\_type};?/  
=\> nil

How to fix it? Thanks a lot.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [July 31, 2009, 11:35am UTC](https://www.ruby-forum.com/t/how-to-include-a-string-into-a-regex-comparison/173016/2 "2009-07-31T11:35:02Z")

</div>

2009/7/31 IÃ±aki Baz C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> =\> nil
> 
> How to fix it? Thanks a lot.

Solved:

received\_content\_type =~ /^#{Regex.escape(received\_content\_type)};?/  
=\> 0

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [July 31, 2009, 11:45am UTC](https://www.ruby-forum.com/t/how-to-include-a-string-into-a-regex-comparison/173016/3 "2009-07-31T11:45:34Z")

</div>

Le 31 juillet à 11:25, Iñaki Baz C. a écrit :

> Hi, I don’t get solving the following issue (sure it’s easy but I cannot do it):

> How to fix it? Thanks a lot.

> valid\_content\_type = “application/auth-policy+xml”

When you insert a string inside a regexp, it’s evaluated as is by the  
regexp engine. You need to escape the special characters (here, the +),  
by prepending backslashes or asking the Regexp class to do it for you :

> > received\_content\_type =~ /^#{Regexp::escape(received\_content\_type)};?/  
> > =\> 0

Or :

> > valid\_content\_type = Regexp::escape(“application/auth-policy+xml”)  
> > =\> “application/auth\-policy\+xml”
> 
> > received\_content\_type =~ /^#{received\_content\_type};?/  
> > =\> 0

Fred

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [July 31, 2009, 11:46am UTC](https://www.ruby-forum.com/t/how-to-include-a-string-into-a-regex-comparison/173016/4 "2009-07-31T11:46:44Z")

</div>

On Fri, Jul 31, 2009 at 11:25 AM, Iñaki Baz C.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> valid\_content\_type = “application/auth-policy+xml”  
> received\_content\_type = “application/auth-policy+xml;charset=ISO-8859-1”
> 
> received\_content\_type =~ /^application/auth-policy+xml;?/  
> =\> 0
> 
> received\_content\_type =~ /^#{received\_content\_type};?/  
> =\> nil
> 
> How to fix it? Thanks a lot.

Try this:

irb(main):001:0\> valid\_content\_type = “application/auth-policy+xml”  
=\> “application/auth-policy+xml”  
irb(main):002:0\> received\_content\_type =  
“application/auth-policy+xml;charset=ISO-8859-1”  
=\> “application/auth-policy+xml;charset=ISO-8859-1”  
irb(main):005:0\> re = /^#{Regexp.escape(valid\_content\_type)};?/  
=\> /^application/auth-policy+xml;?/  
irb(main):006:0\> received\_content\_type =~ re  
=\> 0

Hope this helps,

Jesus.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [July 31, 2009, 11:49am UTC](https://www.ruby-forum.com/t/how-to-include-a-string-into-a-regex-comparison/173016/5 "2009-07-31T11:49:05Z")

</div>

El 31 de julio de 2009 11:45, JesÃºs Gabriel y  
GalÃ¡n[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) escribiÃ³:

> =\> 0  
> Thanks to all, the fact is that I already solved the issue doing  
> exactly the same 🙂  
> I replied to myself in the maillist.

Thanks a lot for all your help.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [July 31, 2009, 11:47am UTC](https://www.ruby-forum.com/t/how-to-include-a-string-into-a-regex-comparison/173016/6 "2009-07-31T11:47:54Z")

</div>

On Fri, Jul 31, 2009 at 11:34 AM, Iñaki Baz C.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 2009/7/31 Iñaki Baz C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Solved:
> 
> received\_content\_type =~ /^#{Regex.escape(received\_content\_type)};?/  
> =\> 0

Oh, sorry, I missed you already solved it. Anyway I wanted to point  
out that maybe what you want in the Regex.escape is valid\_content\_type  
not received\_content\_type…

Jesus.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [July 31, 2009, 11:50am UTC](https://www.ruby-forum.com/t/how-to-include-a-string-into-a-regex-comparison/173016/7 "2009-07-31T11:50:50Z")

</div>

El 31 de julio de 2009 11:46, JesÃºs Gabriel y  
GalÃ¡n[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) escribiÃ³:

> not received\_content\_type…  
> Oh yes, sure, it was a typo just in the mail, in my code I did it  
> correctly 🙂

Thanks a lot.
