Forum: Ruby Regexp problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
699c00ad35f2755810b4aa5f423d73e2?d=identicon&s=25 Albert Schlef (alby)
on 2009-03-10 08:19
I want to extract an "id" parameter from the URL. I use the following
code:

  url = "http://example.com/id=12345"
  id = url[ /(?<=id=)(\d+)/ ]
  puts id

But I get a SyntaxError telling me "undefined (?...) sequence".

Why?
Beb77c4602c3cac7a12149431366ed11?d=identicon&s=25 The Higgs bozo (higgsbozo)
on 2009-03-10 08:43
Albert Schlef wrote:
> I want to extract an "id" parameter from the URL. I use the following
> code:
>
>   url = "http://example.com/id=12345"
>   id = url[ /(?<=id=)(\d+)/ ]
>   puts id
>
> But I get a SyntaxError telling me "undefined (?...) sequence".
>
> Why?

Because ruby 1.8 does not have look-behind.  1.9 has it, and your code
runs on 1.9.

If you need 1.8 with look-behind, I guess you can compile 1.8 with the
Oniguruma regexp engine (which is used in 1.9).
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2009-03-10 09:16
(Received via mailing list)
On Mar 10, 2009, at 00:17 , Albert Schlef wrote:

> I want to extract an "id" parameter from the URL. I use the following
> code:
>
>  url = "http://example.com/id=12345"
>  id = url[ /(?<=id=)(\d+)/ ]
>  puts id
>
> But I get a SyntaxError telling me "undefined (?...) sequence".

(?:...)

not

(?<...)

but really, /(\d+)/ will suffice for your example. see:

see http://www.zenspider.com/Languages/Ruby/QuickRef.html#11
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-03-10 10:20
Albert Schlef wrote:
> I want to extract an "id" parameter from the URL. I use the following
> code:
>
>   url = "http://example.com/id=12345"
>   id = url[ /(?<=id=)(\d+)/ ]
>   puts id
>
> But I get a SyntaxError telling me "undefined (?...) sequence".
>
> Why?

url = "http://example.com/id=12345"

pieces = url.split("/")
id = pieces[-1].split("=")[1]
puts id

--output:--
12345
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-03-10 10:27
(Received via mailing list)
2009/3/10 The Higgs bozo <higgs.bozo@gmail.com>:
>> Why?
>
> Because ruby 1.8 does not have look-behind.  1.9 has it, and your code
> runs on 1.9.
>
> If you need 1.8 with look-behind, I guess you can compile 1.8 with the
> Oniguruma regexp engine (which is used in 1.9).

Lookbehind is not needed:

09:35:18 ~$ irb
Ruby version 1.8.7
irb(main):001:0> url = "http://example.com/id=12345"
=> "http://example.com/id=12345"
irb(main):002:0> id = url[/id=(\d+)/, 1]
=> "12345"

Depending on what else should be done with the url a more appropriate
tool might be class URI

irb(main):006:0> require 'uri'
=> true
irb(main):007:0> u2 = URI.parse url
=> #<URI::HTTP:0x7ff186bc URL:http://example.com/id=12345>
irb(main):008:0> u2.path
=> "/id=12345"
irb(main):009:0> u2.path[/id=(\d+)/, 1]
=> "12345"

Kind regards

robert
699c00ad35f2755810b4aa5f423d73e2?d=identicon&s=25 Albert Schlef (alby)
on 2009-03-10 11:14
The Higgs bozo wrote:
>>> Why?
>>
>> Because ruby 1.8 does not have look-behind. 1.9 has it, and your code
>> runs on 1.9.

Thanks for the info, Higgs.

Robert Klemme wrote:
> Lookbehind is not needed:
[snip]
> irb(main):002:0> id = url[/id=(\d+)/, 1]
> => "12345"

Cool! I once knew of the second parameter. Thanks for reminding me of
it.

I wish I could use RI (too slooouuuw on my system) or Fast-RI (I have
it, but at one point it decided to start throwing exceptions).
699c00ad35f2755810b4aa5f423d73e2?d=identicon&s=25 Albert Schlef (alby)
on 2009-03-10 11:19
Albert Schlef wrote:
> I wish I could use RI (too slooouuuw on my system) or Fast-RI (I have
> it, but at one point it decided to start throwing exceptions).

Oops. It started *raising* exceptions. Everybody knows that in Ruby you
don't *throw* exceptions ;-)
D15a45a973443d4562051eb675b60474?d=identicon&s=25 Tom Cloyd (Guest)
on 2009-03-10 11:59
(Received via mailing list)
Albert Schlef wrote:
>
I've lately been making excellent use of the  MatchData class, so here's
my solution:

irb(main):012:0> m = /\d+/.match("http://example.com/id=12345")
=> #<MatchData "12345">
irb(main):013:0> m[0]
=> "12345"

In other situations, some of the various class methods can be useful:

irb(main):014:0> m.pre_match
=> "http://example.com/id="
irb(main):015:0>

There's a #post_match method also...and more.

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This topic is locked and can not be replied to.