How to include a string into a regex comparison?

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

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


valid_content_type =3D "application/auth-policy+xml"
received_content_type =3D "application/auth-policy+xml;charset=3DISO-8859-1=
"

received_content_type =3D~ /^application\/auth\-policy\+xml;?/
=3D> 0

received_content_type =3D~ /^#{received_content_type};?/
=3D> nil

How to fix it? Thanks a lot.



--=20
I=C3=B1aki Baz Castillo
<[email protected]>
 
I

Iñaki Baz Castillo

2009/7/31 I=C3=B1aki Baz Castillo said:
Hi, I don't get solving the following issue (sure it's easy but I cannot = do it):


valid_content_type =C2=A0 =C2=A0 =C2=A0=3D "application/auth-policy+xml"
received_content_type =3D "application/auth-policy+xml;charset=3DISO-8859= -1"

received_content_type =3D~ /^application\/auth\-policy\+xml;?/
=3D> 0

received_content_type =3D~ /^#{received_content_type};?/
=3D> nil

How to fix it? Thanks a lot.


Solved:

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


--=20
I=C3=B1aki Baz Castillo
<[email protected]>
 
F

F. Senault

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 :
=> 0

Or :
=> 0

Fred
 
J

Jesús Gabriel y Galán

valid_content_type =A0 =A0 =A0=3D "application/auth-policy+xml"
received_content_type =3D "application/auth-policy+xml;charset=3DISO-8859= -1"

received_content_type =3D~ /^application\/auth\-policy\+xml;?/
=3D> 0

received_content_type =3D~ /^#{received_content_type};?/
=3D> nil

How to fix it? Thanks a lot.

Try this:

irb(main):001:0> valid_content_type =3D "application/auth-policy+xml"
=3D> "application/auth-policy+xml"
irb(main):002:0> received_content_type =3D
"application/auth-policy+xml;charset=3DISO-8859-1"
=3D> "application/auth-policy+xml;charset=3DISO-8859-1"
irb(main):005:0> re =3D /^#{Regexp.escape(valid_content_type)};?/
=3D> /^application\/auth\-policy\+xml;?/
irb(main):006:0> received_content_type =3D~ re
=3D> 0

Hope this helps,

Jesus.
 
J

Jesús Gabriel y Galán

2009/7/31 I=F1aki Baz Castillo <[email protected]>:
Solved:

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

Iñaki Baz Castillo

El 31 de julio de 2009 11:45, Jes=C3=BAs Gabriel y
Gal=C3=A1n said:
Try this:

irb(main):001:0> valid_content_type =C2=A0 =C2=A0 =C2=A0=3D "application/= auth-policy+xml"
=3D> "application/auth-policy+xml"
irb(main):002:0> received_content_type =3D
"application/auth-policy+xml;charset=3DISO-8859-1"
=3D> "application/auth-policy+xml;charset=3DISO-8859-1"
irb(main):005:0> re =3D /^#{Regexp.escape(valid_content_type)};?/
=3D> /^application\/auth\-policy\+xml;?/
irb(main):006:0> received_content_type =3D~ re
=3D> 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.


--=20
I=C3=B1aki Baz Castillo
<[email protected]>
 
I

Iñaki Baz Castillo

El 31 de julio de 2009 11:46, Jes=C3=BAs Gabriel y
Gal=C3=A1n said:
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...

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

Thanks a lot.


--=20
I=C3=B1aki Baz Castillo
<[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top