Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Extract value

Reply
Thread Tools

Extract value

 
 
Manuel Manu00
Guest
Posts: n/a
 
      06-22-2010
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/.

 
Reply With Quote
 
 
 
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      06-22-2010
On Tue, Jun 22, 2010 at 5:36 PM, Manuel Manu00 <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
 
 
 
Matthew Bloch
Guest
Posts: n/a
 
      06-22-2010
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.

--
Matthew



 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      06-22-2010
2010/6/22 Matthew Bloch <(E-Mail Removed)>:
> 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=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/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do i extract vidios when winrar wont extract them??? help plzzzzzzzz smuttdog@sc.rr.com Computer Support 2 12-23-2007 07:03 AM
Extract the integer value from a string (beginner) fool C Programming 7 09-20-2006 09:50 PM
Loop Form - extract display 'value' as well as form value? ASP General 2 02-20-2006 09:36 PM
Extract node based on value eva.mukhija@gmail.com XML 0 08-12-2005 07:37 AM
Re: How do you extract hex value of a Unicode character ? John Lenton Python 0 07-15-2004 12:41 PM



Advertisments