Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > find next occurence, regular expressions

Reply
Thread Tools

find next occurence, regular expressions

 
 
John Griffiths
Guest
Posts: n/a
 
      12-30-2007
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) ?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Stefano Crocco
Guest
Posts: n/a
 
      12-30-2007
Alle domenica 30 dicembre 2007, John Griffiths ha scritto:
> 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) ?


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

 
Reply With Quote
 
 
 
 
Wolfgang Nádasi-Donner
Guest
Posts: n/a
 
      12-30-2007
John Griffiths 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"}
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
find and replace with regular expressions chrispoliquin@gmail.com Python 6 08-01-2008 10:55 AM
CurrentElement->next = CurrentElement->next->next (UNDEFINED?) Deniz Bahar C Programming 2 03-09-2005 12:45 AM
Regular Expressions to find URL's in text SROSeaner ASP General 3 10-11-2004 12:11 AM
Regular Expressions for Find and Replace Mark ASP .Net 0 05-18-2004 01:24 PM
Add custom regular expressions to the validation list of available expressions Jay Douglas ASP .Net 0 08-15-2003 10:19 PM



Advertisments