Help for extracting text with regexp.

Discussion in 'Ruby' started by John Doe, Feb 18, 2011.

  1. John Doe

    John Doe Guest

    Hi,

    I'm having some trouble with the regexp :/

    I'm messing up with the match, scan methods, I'm not sure wich one I
    should use.

    Here's what I'm trying to do :

    I have a string that's an URL with get values :
    Ex :
    foo.bar/somePath/somepage.php?attr=value&theone=64646hs554&other=ddd

    What I need is the value of the "theone" attribute.
    This attribute may or may not be the last one in the string !

    I don't know if it's possible to put the two cases in one regexp...

    Here are my two regexp :

    If it's not the last attribute
    /session=(.*?)&/
    If it's the last attribute
    /session=(.*?)$/

    What should I use next to extract the value ? scan or match ?

    myString.scan(/session=(.*?)&/) ?
    How can I handle the two situation ?

    Thanks a lot for your help !

    (Sorry for my english)

    Pollop.

    --
    Posted via http://www.ruby-forum.com/.
    John Doe, Feb 18, 2011
    #1
    1. Advertising

  2. You know what they say, if you choose to use a regex you've got
    another problem :(

    How about this?

    a = "foo.bar/somePath/somepage.php?attr=value&theone=64646hs554&other=ddd"

    a.split(/\?|&/).each do |b|
    if b =~ /^theone=(.*)/
    puts $1
    end
    end
    Peter Hickman, Feb 18, 2011
    #2
    1. Advertising

  3. On Fri, Feb 18, 2011 at 4:02 PM, John Doe <> wrote:
    > Hi,
    >
    > I'm having some trouble with the regexp :/
    >
    > I'm messing up with the match, scan methods, I'm not sure wich one I
    > should use.
    >
    > Here's what I'm trying to do :
    >
    > I have a string that's an URL with get values :
    > Ex :
    > foo.bar/somePath/somepage.php?attr=value&theone=64646hs554&other=ddd
    >
    > What I need is the value of the "theone" attribute.
    > This attribute may or may not be the last one in the string !
    >
    > I don't know if it's possible to put the two cases in one regexp...
    >
    > Here are my two regexp :
    >
    > If it's not the last attribute
    > /session=(.*?)&/
    > If it's the last attribute
    > /session=(.*?)$/
    >
    > What should I use next to extract the value ? scan or match ?
    >
    > myString.scan(/session=(.*?)&/) ?
    > How can I handle the two situation ?
    >
    > Thanks a lot for your help !
    >
    > (Sorry for my english)


    URI and CGI from the standard library will do the work for you:

    irb(main):001:0> require 'uri'
    => true
    irb(main):002:0> require 'cgi'
    => true
    irb(main):004:0> params =
    CGI.parse(URI.parse("foo.bar/somePath/somepage.php?attr=value&theone=64646hs554&other=ddd").query)
    => {"other"=>["ddd"], "theone"=>["64646hs554"], "attr"=>["value"]}
    irb(main):005:0> params["theone"]
    => ["64646hs554"]
    irb(main):006:0> params["theone"].first
    => "64646hs554"


    Jesus.
    Jesús Gabriel y Galán, Feb 18, 2011
    #3
  4. John Doe

    botp Guest

    On Fri, Feb 18, 2011 at 11:02 PM, John Doe
    <> wrote:
    > foo.bar/somePath/somepage.php?attr=value&theone=64646hs554&other=ddd
    > What I need is the value of the "theone" attribute.


    there are many ways.
    sometimes, we just need to focus on what we need... eg,

    Hash[* "foo.bar/somePath/somepage.php?attr=value&theone=64646hs554&other=ddd".scan(/(\w+)=(\w+)/).flatten]

    #=> {"attr"=>"value", "theone"=>"64646hs554", "other"=>"ddd"}


    best regards -botp
    botp, Feb 18, 2011
    #4
  5. John Doe

    John Doe Guest

    John Doe, Feb 18, 2011
    #5
    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. Replies:
    7
    Views:
    561
    Tom Anderson
    Nov 9, 2005
  2. Alex
    Replies:
    6
    Views:
    456
    Roedy Green
    Feb 1, 2008
  3. Greg Hurrell
    Replies:
    4
    Views:
    149
    James Edward Gray II
    Feb 14, 2007
  4. Joao Silva
    Replies:
    16
    Views:
    339
    7stud --
    Aug 21, 2009
  5. Robert Fentress
    Replies:
    4
    Views:
    99
    Robert Fentress
    Feb 7, 2004
Loading...

Share This Page