# Regexp problem

**URL:** <https://www.ruby-forum.com/t/regexp-problem/162472>\
**Category:** Ruby\
**Created:** [March 10, 2009, 8:19am UTC](https://www.ruby-forum.com/t/regexp-problem/162472 "2009-03-10T08:19:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![alby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85f322/32.png) [@alby](https://www.ruby-forum.com/u/alby)\
**Post date:** [March 10, 2009, 8:19am UTC](https://www.ruby-forum.com/t/regexp-problem/162472/1 "2009-03-10T08:19:30Z")

</div>

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

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

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

Why?

---

<div class="post-metadata">

**Author:** ![alby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85f322/32.png) [@alby](https://www.ruby-forum.com/u/alby)\
**Post date:** [March 10, 2009, 8:43am UTC](https://www.ruby-forum.com/t/regexp-problem/162472/2 "2009-03-10T08:43:34Z")

</div>

Albert S. wrote:

> I want to extract an “id” parameter from the URL. I use the following  
> code:
> 
> url = “[http://example.com/id=12345](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).

---

<div class="post-metadata">

**Author:** ![alby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85f322/32.png) [@alby](https://www.ruby-forum.com/u/alby)\
**Post date:** [March 10, 2009, 9:16am UTC](https://www.ruby-forum.com/t/regexp-problem/162472/3 "2009-03-10T09:16:44Z")

</div>

On Mar 10, 2009, at 00:17 , Albert S. wrote:

> I want to extract an “id” parameter from the URL. I use the following  
> code:
> 
> url = “[http://example.com/id=12345](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 [Ruby | zenspider.com | by ryan davis](http://www.zenspider.com/Languages/Ruby/QuickRef.html#11)

---

<div class="post-metadata">

**Author:** ![alby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85f322/32.png) [@alby](https://www.ruby-forum.com/u/alby)\
**Post date:** [March 10, 2009, 10:27am UTC](https://www.ruby-forum.com/t/regexp-problem/162472/4 "2009-03-10T10:27:37Z")

</div>

2009/3/10 The Higgs bozo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > 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)”  
=\> “[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](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

---

<div class="post-metadata">

**Author:** ![alby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85f322/32.png) [@alby](https://www.ruby-forum.com/u/alby)\
**Post date:** [March 10, 2009, 11:14am UTC](https://www.ruby-forum.com/t/regexp-problem/162472/5 "2009-03-10T11:14:34Z")

</div>

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 K. 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).

---

<div class="post-metadata">

**Author:** ![alby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85f322/32.png) [@alby](https://www.ruby-forum.com/u/alby)\
**Post date:** [March 10, 2009, 10:20am UTC](https://www.ruby-forum.com/t/regexp-problem/162472/6 "2009-03-10T10:20:21Z")

</div>

Albert S. wrote:

> I want to extract an “id” parameter from the URL. I use the following  
> code:
> 
> url = “[http://example.com/id=12345](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](http://example.com/id=12345)”

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

–output:–  
12345

---

<div class="post-metadata">

**Author:** ![alby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85f322/32.png) [@alby](https://www.ruby-forum.com/u/alby)\
**Post date:** [March 10, 2009, 11:59am UTC](https://www.ruby-forum.com/t/regexp-problem/162472/7 "2009-03-10T11:59:38Z")

</div>

Albert S. 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](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=](http://example.com/id=)”  
irb(main):015:0\>

There’s a #post\_match method also…and more.

t.

–

```auto
Tom C., MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)

```

---

<div class="post-metadata">

**Author:** ![alby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85f322/32.png) [@alby](https://www.ruby-forum.com/u/alby)\
**Post date:** [March 10, 2009, 11:19am UTC](https://www.ruby-forum.com/t/regexp-problem/162472/8 "2009-03-10T11:19:36Z")

</div>

Albert S. 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 😉
