How to include a string into a regex comparison?

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.

2009/7/31 Iñaki Baz C. [email protected]:

=> nil

How to fix it? Thanks a lot.

Solved:

received_content_type =~ /^#{Regex.escape(received_content_type)};?/
=> 0

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

On Fri, Jul 31, 2009 at 11:25 AM, Iñaki Baz C.[email protected] 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.

El 31 de julio de 2009 11:45, Jesús Gabriel y
Galán[email protected] escribió:

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

Thanks a lot for all your help.

On Fri, Jul 31, 2009 at 11:34 AM, Iñaki Baz C.[email protected] wrote:

2009/7/31 Iñaki Baz C. [email protected]:

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.

El 31 de julio de 2009 11:46, Jesús Gabriel y
Galán[email protected] escribió:

not received_content_type…
Oh yes, sure, it was a typo just in the mail, in my code I did it
correctly :slight_smile:

Thanks a lot.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs