Regexpression: scanning a special charachters //?

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

   myoverlook://[email protected]

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

Am Mon, 1 Oct 2007 21:05:06 +0900
schrieb kazaam [email protected]:

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)

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

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

2007/10/1, Sebastian H. [email protected]:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs