[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Newbie needs help with escaping regex

Paul Wistrand

2/18/2005 12:56:00 AM

Hi,
I have a simple project that I thought I'd have a crack at in Ruby. I
want to retrieve a list of names from a web page but the Ruby interpreter is
giving me allot of trouble in parsing the pattern for the regular
expression. The pattern I wanted to use was the following :

pattern =
/<span\sclass\s=\s"rankingName"><a\shref="w3xp-player-profile.aspx\?Gateway=
([^&]*)&PlayerName=([^"]*)">((\n|.)*?)</a></span>/

understandably the forward slash in the closing span tag causes Ruby to
think this is the end of the pattern. Hence my question is how the heck do I
escape this?

Thanks
Paul

B.T.W I realise that following works just as well...
pattern =
/<span\sclass\s=\s"rankingName"><a\shref="w3xp-player-profile.aspx\?Gateway=
([^&]*)&PlayerName=([^"]*)">((\n|.)*?)</


5 Answers

Zach Dennis

2/18/2005 1:06:00 AM

0

Paul Wistrand wrote:
> Hi,
> I have a simple project that I thought I'd have a crack at in Ruby. I
> want to retrieve a list of names from a web page but the Ruby interpreter is
> giving me allot of trouble in parsing the pattern for the regular
> expression. The pattern I wanted to use was the following :
>
> pattern =
> /<span\sclass\s=\s"rankingName"><a\shref="w3xp-player-profile.aspx\?Gateway=
> ([^&]*)&PlayerName=([^"]*)">((\n|.)*?)</a></span>/
>
> understandably the forward slash in the closing span tag causes Ruby to
> think this is the end of the pattern. Hence my question is how the heck do I
> escape this?
>
> Thanks
> Paul
>
> B.T.W I realise that following works just as well...
> pattern =
> /<span\sclass\s=\s"rankingName"><a\shref="w3xp-player-profile.aspx\?Gateway=
> ([^&]*)&PlayerName=([^"]*)">((\n|.)*?)</

Just add a \ slash before all characters you want to escape, in this
case the forward slashes.

example:
rgx = /<\/span>/

HTH,

Zach


Logan Capaldo

2/18/2005 1:15:00 AM

0

On Fri, 18 Feb 2005 10:06:04 +0900, Zach Dennis <zdennis@mktec.com> wrote:
> Paul Wistrand wrote:
> > Hi,
> > I have a simple project that I thought I'd have a crack at in Ruby. I
> > want to retrieve a list of names from a web page but the Ruby interpreter is
> > giving me allot of trouble in parsing the pattern for the regular
> > expression. The pattern I wanted to use was the following :
> >
> > pattern =
> > /<span\sclass\s=\s"rankingName"><a\shref="w3xp-player-profile.aspx\?Gateway=
> > ([^&]*)&PlayerName=([^"]*)">((\n|.)*?)</a></span>/
> >
> > understandably the forward slash in the closing span tag causes Ruby to
> > think this is the end of the pattern. Hence my question is how the heck do I
> > escape this?
> >
> > Thanks
> > Paul
> >
> > B.T.W I realise that following works just as well...
> > pattern =
> > /<span\sclass\s=\s"rankingName"><a\shref="w3xp-player-profile.aspx\?Gateway=
> > ([^&]*)&PlayerName=([^"]*)">((\n|.)*?)</
>
> Just add a \ slash before all characters you want to escape, in this
> case the forward slashes.
>
> example:
> rgx = /<\/span>/
>
> HTH,
>
> Zach
>
>

An alternative is to use %r{} syntax.

%r{(now|I)can (use)+ as many /* as I? want}

Of course the downside being is now you have to escape { }. But your
pattern doesn't seem to contain any of those anyway.


Martin DeMello

2/18/2005 8:22:00 AM

0

Logan Capaldo <logancapaldo@gmail.com> wrote:

> An alternative is to use %r{} syntax.
>
> %r{(now|I)can (use)+ as many /* as I? want}
>
> Of course the downside being is now you have to escape { }. But your
> pattern doesn't seem to contain any of those anyway.

%r can take a pretty much arbitrary delimiter (I forget what isn't
allowed). e.g. %r|hello| %r<world> %r=this= etc. The first character
after the r acts as the opening delimiter, if it's one of a pair the
other one acts as the closing, otherwise the opening character closes it
too.

martin

Paul Wistrand

2/18/2005 11:23:00 AM

0

LOL. As soon as I saw your post I thought to myself "but I tried that". Then
it dawned on me that in my tied state I had escaped the forward slash in the
closing </span> tag but not the </a>! Thanks.


"Zach Dennis" <zdennis@mktec.com> wrote in message
news:42153F6D.6030006@mktec.com...
> Paul Wistrand wrote:
> > Hi,
> > I have a simple project that I thought I'd have a crack at in Ruby.
I
> > want to retrieve a list of names from a web page but the Ruby
interpreter is
> > giving me allot of trouble in parsing the pattern for the regular
> > expression. The pattern I wanted to use was the following :
> >
> > pattern =
> >
/<span\sclass\s=\s"rankingName"><a\shref="w3xp-player-profile.aspx\?Gateway=
> > ([^&]*)&PlayerName=([^"]*)">((\n|.)*?)</a></span>/
> >
> > understandably the forward slash in the closing span tag causes Ruby to
> > think this is the end of the pattern. Hence my question is how the heck
do I
> > escape this?
> >
> > Thanks
> > Paul
> >
> > B.T.W I realise that following works just as well...
> > pattern =
> >
/<span\sclass\s=\s"rankingName"><a\shref="w3xp-player-profile.aspx\?Gateway=
> > ([^&]*)&PlayerName=([^"]*)">((\n|.)*?)</
>
> Just add a \ slash before all characters you want to escape, in this
> case the forward slashes.
>
> example:
> rgx = /<\/span>/
>
> HTH,
>
> Zach
>
>


Florian Gross

2/18/2005 12:56:00 PM

0

Logan Capaldo wrote:

> An alternative is to use %r{} syntax.
>
> %r{(now|I)can (use)+ as many /* as I? want}
>
> Of course the downside being is now you have to escape { }. But your
> pattern doesn't seem to contain any of those anyway.

It's not a problem as long as they are balanced:

irb(main):004:0> %r{foo{0,1}}
=> /foo{0,1}/

So %r with one of the paired delimiters is usually a pretty good choice
if you want to avoid escaping. Especially since you have to escape (, ),
{ and } in Regexps anyway if they don't appear in one of the meta
constructs in which case they would be balanced anyway.