regexpression: scanning a special charachters // ?

K

kazaam

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
 
M

Markus Schirp

Am Mon, 1 Oct 2007 21:05:06 +0900
schrieb kazaam said:
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)
 
S

Sebastian Hungerecker

kazaam said:
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
 
K

kazaam

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

Robert Klemme

2007/10/1 said:
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
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top