Extract value

M

Manuel Manu00

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
 
J

Jesús Gabriel y Galán

Hello

I would like to parse or regexp the following string :

<script type=3D"text/javascript">
=A0google_ad_client =3D "pub-9423056098431875";
=A0/* 300x250, created 6/10/09 */
=A0google_ad_slot =3D "1755518182";
=A0google_ad_width =3D 300;
=A0google_ad_height =3D 250;
=A0</script>

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

I tried with regexp : ((google_ad_client =3D \"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 =3D<<EOF
irb(main):002:0" <script type=3D"text/javascript">
irb(main):003:0" google_ad_client =3D "pub-9423056098431875";
irb(main):004:0" /* 300x250, created 6/10/09 */
irb(main):005:0" google_ad_slot =3D "1755518182";
irb(main):006:0" google_ad_width =3D 300;
irb(main):007:0" google_ad_height =3D 250;
irb(main):008:0" </script>
irb(main):009:0" EOF
=3D> "<script type=3D\"text/javascript\">\n google_ad_client =3D
\"pub-9423056098431875\";\n /* 300x250, created 6/10/09 */\n
google_ad_slot =3D \"1755518182\";\n google_ad_width =3D 300;\n
google_ad_height =3D 250;\n </script>\n"
irb(main):011:0> m =3D doc.match /google_ad_client =3D "(.*?)"/
=3D> #<MatchData "google_ad_client =3D \"pub-9423056098431875\""
1:"pub-9423056098431875">
irb(main):012:0> m.captures
=3D> ["pub-9423056098431875"]


Jesus.
 
M

Matthew Bloch

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.
 
R

Robert Klemme

2010/6/22 Matthew Bloch said:
Hello

I would like to parse or regexp the following string :

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

matched =3D /google_ad_client\s*=3D\s*\"([^"]+)\"/.match(string)
google_ad_client =3D 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=3D<<STR
irb(main):002:0" <script type=3D"text/javascript">
irb(main):003:0" google_ad_client =3D "pub-9423056098431875";
irb(main):004:0" /* 300x250, created 6/10/09 */
irb(main):005:0" google_ad_slot =3D "1755518182";
irb(main):006:0" google_ad_width =3D 300;
irb(main):007:0" google_ad_height =3D 250;
irb(main):008:0" </script>
irb(main):009:0" STR
=3D> "<script type=3D\"text/javascript\">\n google_ad_client =3D
\"pub-9423056098431875\";\n /* 300x250, created 6/10/09 */\n
google_ad_slot =3D \"1755518182\";\n google_ad_width =3D 300;\n
google_ad_height =3D 250;\n </script>\n"


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

If there is no match, you get nil.

Kind regards

robert

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top