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.

···

--
Iñaki Baz Castillo
<ibc@aliax.net>

Solved:

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

···

2009/7/31 Iñaki Baz Castillo <ibc@aliax.net>:

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.

--
Iñaki Baz Castillo
<ibc@aliax.net>

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

···

Le 31 juillet à 11:25, Iñaki Baz Castillo a écrit :
--
I wonder why, when I just did kind of normal things -- some good
engineering and just what I wanted to do in life -- why everywhere I go,
some people think that I'm some kind of hero or a special person.
                                                         (Steve Wozniak)

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.

···

On Fri, Jul 31, 2009 at 11:25 AM, Iñaki Baz Castillo<ibc@aliax.net> 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.

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.

···

On Fri, Jul 31, 2009 at 11:34 AM, Iñaki Baz Castillo<ibc@aliax.net> wrote:

2009/7/31 Iñaki Baz Castillo <ibc@aliax.net>:

Solved:

received_content_type =~ /^#{Regex.escape(received_content_type)};?/
=> 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.

···

El 31 de julio de 2009 11:45, Jesús Gabriel y Galán<jgabrielygalan@gmail.com> escribió:

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

--
Iñaki Baz Castillo
<ibc@aliax.net>

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

Thanks a lot.

···

El 31 de julio de 2009 11:46, Jesús Gabriel y Galán<jgabrielygalan@gmail.com> escribió:

On Fri, Jul 31, 2009 at 11:34 AM, Iñaki Baz Castillo<ibc@aliax.net> wrote:

2009/7/31 Iñaki Baz Castillo <ibc@aliax.net>:

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...

--
Iñaki Baz Castillo
<ibc@aliax.net>