# Find next occurence, regular expressions

**URL:** <https://www.ruby-forum.com/t/find-next-occurence-regular-expressions/124574>\
**Category:** Ruby\
**Created:** [December 30, 2007, 1:47pm UTC](https://www.ruby-forum.com/t/find-next-occurence-regular-expressions/124574 "2007-12-30T13:47:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [December 30, 2007, 1:47pm UTC](https://www.ruby-forum.com/t/find-next-occurence-regular-expressions/124574/1 "2007-12-30T13:47:42Z")

</div>

Hi, ok I’m none to good at regular expressions but i’ve got to do  
something and need a little help.

Basically i’m trying to hunt down a particular word within a url string,

so to test, i define a local variable, var\_string,

var\_string=“user=22&val=999”

then run a regular expression match on the variable,

user = var\_string.match(/=(\w+)&/)

which returns =\> ‘22’ as it’s looking for the first word occurence which  
has = and a & at the start & finish.

so the question is how could i return the second occurence (999) ?

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [December 30, 2007, 1:59pm UTC](https://www.ruby-forum.com/t/find-next-occurence-regular-expressions/124574/2 "2007-12-30T13:59:49Z")

</div>

Alle domenica 30 dicembre 2007, John G. ha scritto:

> user = var\_string.match(/=(\w+)&/)
> 
> which returns =\> ‘22’ as it’s looking for the first word occurence which  
> has = and a & at the start & finish.
> 
> so the question is how could i return the second occurence (999) ?

In ruby 1.8, String#match always returns the first match. If you need to  
find  
other matches, you can look at StringScanner or String#scan (of course,  
you’ll need to remove the & from the regexp, or make it optional, if you  
want  
to match the string ‘999’. To make the ‘&’ optional, put a ? after  
it: /=(\w+)&?).

In ruby 1.9, instead String#match should accept an offset (as far as I  
know,  
the documentation doesn’t mention this, but it seems to work). You  
should  
still modify the regexp to make it work.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [December 30, 2007, 2:35pm UTC](https://www.ruby-forum.com/t/find-next-occurence-regular-expressions/124574/3 "2007-12-30T14:35:54Z")

</div>

John G. wrote:

> Hi, ok I’m none to good at regular expressions but i’ve got to do  
> something and need a little help.
> 
> Basically i’m trying to hunt down a particular word within a url string,
> 
> so to test, i define a local variable, var\_string,
> 
> var\_string=“user=22&val=999”
> 
> then run a regular expression match on the variable,
> 
> user = var\_string.match(/=(\w+)&/)
> 
> which returns =\> ‘22’ as it’s looking for the first word occurence which  
> has = and a & at the start & finish.
> 
> so the question is how could i return the second occurence (999) ?

I think this will be an easy solution…

var\_string=“user=22&val=999”  
vars = {}  
var\_string.scan(/(\w+)=(\w+)(?:&|$)/) do |name, val|  
vars[name] = val  
end  
p vars # =\> {“user”=\>“22”, “val”=\>“999”}
