Help for extracting text with regexp


I’m having some trouble with the regexp :confused:

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 :

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
If it’s the last attribute

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)


You know what they say, if you choose to use a regex you’ve got
another problem :frowning:

How about this?

a =

a.split(/?|&/).each do |b|
if b =~ /^theone=(.*)/
puts $1

On Fri, Feb 18, 2011 at 11:02 PM, John D.
[email protected] wrote:
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,


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

best regards -botp

On Fri, Feb 18, 2011 at 4:02 PM, John D.
[email protected] wrote:

Ex :
(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 =
=> {“other”=>[“ddd”], “theone”=>[“64646hs554”], “attr”=>[“value”]}
irb(main):005:0> params[“theone”]
=> [“64646hs554”]
irb(main):006:0> params[“theone”].first
=> “64646hs554”


Thanks a lot for your help ! :smiley:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs