[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Regexp problem

Albert Schlef

3/10/2009 7:18:00 AM

I want to extract an "id" parameter from the URL. I use the following
code:

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

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

Why?
--
Posted via http://www.ruby-....

7 Answers

The Higgs bozo

3/10/2009 7:42:00 AM

0

Albert Schlef wrote:
> I want to extract an "id" parameter from the URL. I use the following
> code:
>
> url = "http://example.com/id=1...
> 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).
--
Posted via http://www.ruby-....

Ryan Davis

3/10/2009 8:14:00 AM

0


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=1...
> 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/QuickR...



7stud --

3/10/2009 9:19:00 AM

0

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

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

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

--output:--
12345

--
Posted via http://www.ruby-....

Robert Klemme

3/10/2009 9:25:00 AM

0

2009/3/10 The Higgs bozo <higgs.bozo@gmail.com>:
> Albert Schlef wrote:
>> I want to extract an "id" parameter from the URL. I use the following
>> code:
>>
>> =A0 url =3D "http://example.com/id=3D1...
>> =A0 id =3D url[ /(?<=3Did=3D)(\d+)/ ]
>> =A0 puts id
>>
>> But I get a SyntaxError telling me "undefined (?...) sequence".
>>
>> Why?
>
> Because ruby 1.8 does not have look-behind. =A01.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 =3D "http://example.com/id=3D1...
=3D> "http://example.com/id=3D1...
irb(main):002:0> id =3D url[/id=3D(\d+)/, 1]
=3D> "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'
=3D> true
irb(main):007:0> u2 =3D URI.parse url
=3D> #<URI::HTTP:0x7ff186bc URL:http://example.com/id=3...
irb(main):008:0> u2.path
=3D> "/id=3D12345"
irb(main):009:0> u2.path[/id=3D(\d+)/, 1]
=3D> "12345"

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end

Albert Schlef

3/10/2009 10:13:00 AM

0

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).
--
Posted via http://www.ruby-....

Albert Schlef

3/10/2009 10:18:00 AM

0

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 ;-)

--
Posted via http://www.ruby-....

Tom Cloyd

3/10/2009 10:57:00 AM

0

Albert Schlef wrote:
> I want to extract an "id" parameter from the URL. I use the following
> code:
>
> url = "http://example.com/id=1...
> id = url[ /(?<=id=)(\d+)/ ]
> puts id
>
> But I get a SyntaxError telling me "undefined (?...) sequence".
>
> Why?
>
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=1...)
=> #<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...
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)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~