Extract value

Hello

I would like to parse or regexp the following string :

in order to keep only this : pub-9423056098431875 (and store it in a
base)

I tried with regexp : ((google_ad_client = “pub-)(.*?)(”;))

but it’s not working. and i don’t know which function to use (split ?
splice ?) and how to proceed… I’m n00b in Ruby, sorry.

Thank you very much for your help

On Tue, Jun 22, 2010 at 5:36 PM, Manuel Manu00 [email protected] wrote:

in order to keep only this : pub-9423056098431875 (and store it in a
base)

I tried with regexp : ((google_ad_client = "pub-)(.*?)(";))

but it’s not working. and i don’t know which function to use (split ?
splice ?) and how to proceed… I’m n00b in Ruby, sorry.

Try this:

irb(main):001:0> doc =<<EOF
irb(main):002:0"
irb(main):009:0" EOF
=> "<script type="text/javascript">\n google_ad_client =
"pub-9423056098431875";\n / 300x250, created 6/10/09 /\n
google_ad_slot = "1755518182";\n google_ad_width = 300;\n
google_ad_height = 250;\n \n"
irb(main):011:0> m = doc.match /google_ad_client = "(.
?)"/
=> #<MatchData “google_ad_client = "pub-9423056098431875"”
1:“pub-9423056098431875”>
irb(main):012:0> m.captures
=> [“pub-9423056098431875”]

Jesus.

2010/6/22 Matthew B. [email protected]:

google_ad_height = 250;

matched = /google_ad_client\s*=\s*"([^"]+)"/.match(string)
google_ad_client = matched ? matched[1] : nil

seems flexible-ish, and might do what you want.

You can also use String#[] like this:

irb(main):001:0> s=<<STR
irb(main):002:0"
irb(main):009:0" STR
=> "<script type="text/javascript">\n google_ad_client =
"pub-9423056098431875";\n / 300x250, created 6/10/09 */\n
google_ad_slot = "1755518182";\n google_ad_width = 300;\n
google_ad_height = 250;\n \n"

irb(main):010:0> s[/google_ad_client\s*=\s*“([^”]*)"/, 1]
=> “pub-9423056098431875”
irb(main):011:0>

If there is no match, you get nil.

Kind regards

robert

On Wed, 2010-06-23 at 00:36 +0900, Manuel Manu00 wrote:

matched = /google_ad_client\s*=\s*"([^"]+)"/.match(string)
google_ad_client = matched ? matched[1] : nil

seems flexible-ish, and might do what you want.