logo       

Re: How to include a string into a regex comparison?: msg#02362

ruby-talk

Subject: Re: How to include a string into a regex comparison?

Le 31 juillet à 11:25, Iñaki Baz Castillo 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
--
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)

Google Custom Search

News | Mail Home | sitemap | FAQ | advertise