find next occurence, regular expressions

Discussion in 'Ruby' started by John Griffiths, Dec 30, 2007.

  1. 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/.
     
    John Griffiths, Dec 30, 2007
    #1
    1. Advertising

  2. 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
     
    Stefano Crocco, Dec 30, 2007
    #2
    1. Advertising

  3. 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/.
     
    Wolfgang Nádasi-Donner, Dec 30, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dustin D.
    Replies:
    1
    Views:
    11,217
  2. natG
    Replies:
    4
    Views:
    9,676
  3. Sean Berry
    Replies:
    10
    Views:
    690
  4. Deniz Bahar
    Replies:
    2
    Views:
    480
    Andrey Tarasevich
    Mar 9, 2005
  5. Noman Shapiro
    Replies:
    0
    Views:
    235
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page