Extract value

Discussion in 'Ruby' started by Manuel Manu00, Jun 22, 2010.

  1. 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/.
    Manuel Manu00, Jun 22, 2010
    #1
    1. Advertising

  2. On Tue, Jun 22, 2010 at 5:36 PM, Manuel Manu00 <> 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.
    Jesús Gabriel y Galán, Jun 22, 2010
    #2
    1. Advertising

  3. 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
    Matthew Bloch, Jun 22, 2010
    #3
  4. 2010/6/22 Matthew Bloch <>:
    > 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/
    Robert Klemme, Jun 22, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    381
  2. John Lenton
    Replies:
    0
    Views:
    405
    John Lenton
    Jul 15, 2004
  3. fool
    Replies:
    7
    Views:
    410
    Mark McIntyre
    Sep 20, 2006
  4. Replies:
    2
    Views:
    267
  5. Guest
    Replies:
    2
    Views:
    130
    Bob Barrows [MVP]
    Feb 20, 2006
Loading...

Share This Page