[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

regexpression: scanning a special charachters // ?

mrpink

10/1/2007 12:03:00 PM

Hi again,
I need to parse a line like this here:

myoverlook://thatstrue@blablabla

I need at the end just thatstrue. So I wrote for the beginning:

result = overlook.scan(/myoverlook://(.+?)\n/m)

Of course this doesn't work because of the // which are special charachters in a regexpr. But how can I tell that they shoudln't be treated as specialcharachters? Is there an escape charachter? I tried it so:

result = overlook.scan(/myoverlook:\/\/\(.+?)\n/m)

but this throws an error: unmatched ): /myoverlook:\/\/\(.+?)\n/

What's the correct synthax for this?

bye
--
kazaam <kazaam@oleco.net>
4 Answers

Markus Schirp

10/1/2007 12:14:00 PM

0

Am Mon, 1 Oct 2007 21:05:06 +0900
schrieb kazaam <kazaam@oleco.net>:

> Hi again,
> I need to parse a line like this here:
>
> myoverlook://thatstrue@blablabla
>
> I need at the end just thatstrue. So I wrote for the beginning:
>
> result = overlook.scan(/myoverlook://(.+?)\n/m)
>
> Of course this doesn't work because of the // which are special
> charachters in a regexpr. But how can I tell that they shoudln't be
> treated as specialcharachters? Is there an escape charachter? I tried
> it so:
>
> result = overlook.scan(/myoverlook:\/\/\(.+?)\n/m)
>
> but this throws an error: unmatched ): /myoverlook:\/\/\(.+?)\n/
>
> What's the correct synthax for this?
>
> bye

You escaped the ( to, so that your cloasing ) is unmatched...

/myoverlook:\/\/\(.+?)\n/m <- yours
/myoverlook:\/\/(.+?)\n/m <- more correct (untested)

Sebastian Hungerecker

10/1/2007 1:13:00 PM

0

kazaam wrote:
> Is there an escape charachter? I tried it so:
>
> result = overlook.scan(/myoverlook:\/\/\(.+?)\n/m)

That would be correct if you hadn't accidentally escaped the (, too.
A cleaner way would be to use %r{} instead of //, so that / isn't treated as a
special character and you don't need to escape it. Like so:
result = overlook.scan(%r{myoverlook://(.+?)\n}m)


HTH,
Sebastian
--
NP: Anathema - Sweet Tears
Jabber: sepp2k@jabber.org
ICQ: 205544826

mrpink

10/1/2007 1:16:00 PM

0

thanks for both your answers. I had thought I had to surround a / with two \ to escape it. Now it works :)


--
kazaam <kazaam@oleco.net>

Robert Klemme

10/1/2007 1:19:00 PM

0

2007/10/1, Sebastian Hungerecker <sepp2k@googlemail.com>:
> kazaam wrote:
> > Is there an escape charachter? I tried it so:
> >
> > result = overlook.scan(/myoverlook:\/\/\(.+?)\n/m)
>
> That would be correct if you hadn't accidentally escaped the (, too.
> A cleaner way would be to use %r{} instead of //, so that / isn't treated as a
> special character and you don't need to escape it. Like so:
> result = overlook.scan(%r{myoverlook://(.+?)\n}m)

I'd also change the RX to a more appropriate one, e.g. any of these

%r{myoverlook://(\w+)@}
%r{myoverlook://([^@]+}

You can then extract the part you are looking for from group 1.

Depends on the surrounding context which of the RX is better suited.
I'd probably prefer the fist one since it is more robust.

Kind regards

robert