Extract value

Hello

I would like to parse or regexp the following string :

<script type="text/javascript">
  google_ad_client = "pub-9423056098431875";
  /* 300x250, created 6/10/09 */
  google_ad_slot = "1755518182";
  google_ad_width = 300;
  google_ad_height = 250;
  </script>

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

···

--
Posted via http://www.ruby-forum.com/.

Try this:

irb(main):001:0> doc =<<EOF
irb(main):002:0" <script type="text/javascript">
irb(main):003:0" google_ad_client = "pub-9423056098431875";
irb(main):004:0" /* 300x250, created 6/10/09 */
irb(main):005:0" google_ad_slot = "1755518182";
irb(main):006:0" google_ad_width = 300;
irb(main):007:0" google_ad_height = 250;
irb(main):008:0" </script>
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 </script>\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.

···

On Tue, Jun 22, 2010 at 5:36 PM, Manuel Manu00 <manu@nebrock.net> wrote:

Hello

I would like to parse or regexp the following string :

<script type="text/javascript">
google_ad_client = "pub-9423056098431875";
/* 300x250, created 6/10/09 */
google_ad_slot = "1755518182";
google_ad_width = 300;
google_ad_height = 250;
</script>

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.

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

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

···

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

Hello

I would like to parse or regexp the following string :

<script type="text/javascript">
  google_ad_client = "pub-9423056098431875";
  /* 300x250, created 6/10/09 */
  google_ad_slot = "1755518182";
  google_ad_width = 300;
  google_ad_height = 250;
  </script>

--
Matthew

You can also use String# like this:

irb(main):001:0> s=<<STR
irb(main):002:0" <script type="text/javascript">
irb(main):003:0" google_ad_client = "pub-9423056098431875";
irb(main):004:0" /* 300x250, created 6/10/09 */
irb(main):005:0" google_ad_slot = "1755518182";
irb(main):006:0" google_ad_width = 300;
irb(main):007:0" google_ad_height = 250;
irb(main):008:0" </script>
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 </script>\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

···

2010/6/22 Matthew Bloch <matthew@bytemark.co.uk>:

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

Hello

I would like to parse or regexp the following string :

<script type="text/javascript">
google_ad_client = "pub-9423056098431875";
/* 300x250, created 6/10/09 */
google_ad_slot = "1755518182";
google_ad_width = 300;
google_ad_height = 250;
</script>

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

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/