regex group match

Discussion in 'Ruby' started by Thufir, Jan 30, 2010.

  1. Thufir

    Thufir Guest

    At www.rubular.com/ the regular expression:

    SID=([\s\S]*?)LSID=([\s\S]*?)Auth=([\s\S]*)

    run against the string below results in three captures. How do I
    replicate these results from Ruby? Part of the trick being that SID and
    LSID are similar, and presumably, may be returned in any order.

    This comes from:

    http://code.google.com/apis/gdata/articles/using_cURL.html#authenticating-
    clientlogin



    thufir@ARRAKIS:~/projects/curl$
    thufir@ARRAKIS:~/projects/curl$ ruby regex.rb
    SID=
    thufir@ARRAKIS:~/projects/curl$
    thufir@ARRAKIS:~/projects/curl$ nl regex.rb

    1 # SID=([\s\S]*?)LSID=([\s\S]*?)Auth=([\s\S]*)
    2 # works at rubular to capture the three sub-strings



    3 s =
    "SID=DQAAAHYBADCv2pSv7nflacDNwz3zEDUGtrSvNVDcpkSfddi77b3U5sEaHmP8YLWhmA36F9rk85mL8J5dqo4apn0T1vKz0fPGI9Xtnuet6cuE2ZzYvrNIwbSC_HjTqF4zudNQnnlDuD2wqZT-
    g1qXI8KhGAQZV4NexHZoQPlabTsGuRZeIBxj1A
    4 LSID=EUBBBIaBADCl-kNxvRVmcQghpt3cqSMfEooKR9flLOUZqwgP9OrZS83gse-
    KSdTNeXhxsET7FYenDhceP9lIPOmesH-t9qh-AWUHjjMdZEbUNeF9mWyzln6Z-FajaiG-
    cVFkqW0ZJ8ZbnCP30xXj6xFK6QxaAcqy_9Pej8jhEnxS9E61ftQGPg
    5 Auth=EUBBIacAAADK-kNxvRVmcQghpt3cqSMfEooLNMflLNIQqwgP9OrZS83gs-
    KSdTNeXhxsET7FYePWmaD8Vsy1V4LSUGMUP48Je2TO8OcjBj6HgAtPhiZeX-
    gKDfagZDK44j4n-Tkb44nhOnp2_QPSnBj3Z2vYwOEDjjG3Q53aQVC2132JKOuGh"


    6 #s = "Here is a string with http://www.mydomain.com/path/to/
    mypage.html in it"
    7 #r = /http:\/\/([a-z.]*)(\/[a-z]*)*(\/[a-z]*.html)/i
    8 #m = r.match s
    9 #p m.string



    10 sid = /SID=[\s\S]*?/

    11 puts sid.match s
    thufir@ARRAKIS:~/projects/curl$



    thanks,

    Thufir
    Thufir, Jan 30, 2010
    #1
    1. Advertising

  2. Thufir

    Thufir Guest

    Solution:

    thufir@ARRAKIS:~/projects/curl$
    thufir@ARRAKIS:~/projects/curl$ nl regex.rb

    1 # SID=([\s\S]*?)LSID=([\s\S]*?)Auth=([\s\S]*)
    2 # http://rubular.com/regexes/13295
    3 # http://code.google.com/apis/gdata/articles/using_cURL.html#authenticating-clientlogin


    4 str =
    "SID=DQAAAHYBADCv2pSv7nflacDNwz3zEDUGtrSvNVDcpkSfddi77b3U5sEaHmP8YLWhmA36F9rk85mL8J5dqo4apn0T1vKz0fPGI9Xtnuet6cuE2ZzYvrNIwbSC_HjTqF4zudNQnnlDuD2wqZT-
    g1qXI8KhGAQZV4NexHZoQPlabTsGuRZeIBxj1A
    5 LSID=EUBBBIaBADCl-kNxvRVmcQghpt3cqSMfEooKR9flLOUZqwgP9OrZS83gse-
    KSdTNeXhxsET7FYenDhceP9lIPOmesH-t9qh-AWUHjjMdZEbUNeF9mWyzln6Z-FajaiG-
    cVFkqW0ZJ8ZbnCP30xXj6xFK6QxaAcqy_9Pej8jhEnxS9E61ftQGPg
    6 Auth=EUBBIacAAADK-kNxvRVmcQghpt3cqSMfEooLNMflLNIQqwgP9OrZS83gs-
    KSdTNeXhxsET7FYePWmaD8Vsy1V4LSUGMUP48Je2TO8OcjBj6HgAtPhiZeX-
    gKDfagZDK44j4n-Tkb44nhOnp2_QPSnBj3Z2vYwOEDjjG3Q53aQVC2132JKOuGh"


    7 m = str.match(/SID=([\s\S]*?)LSID=([\s\S]*?)Auth=([\s\S]
    *)/).captures


    8 puts "SID\n" + m[0]
    9 3.times {puts "*********"}
    10 puts "LSID\n" + m[1]
    11 3.times {puts "*********"}
    12 puts "Auth\n" + m[2]
    13 3.times {puts "*********"}

    thufir@ARRAKIS:~/projects/curl$



    -Thufir
    Thufir, Jan 30, 2010
    #2
    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. darrel
    Replies:
    0
    Views:
    298
    darrel
    Jul 8, 2004
  2. hiwa
    Replies:
    0
    Views:
    630
  3. christopher taylor
    Replies:
    0
    Views:
    396
    christopher taylor
    Sep 17, 2008
  4. RolfK
    Replies:
    1
    Views:
    1,835
    Martin Honnen
    Jun 7, 2009
  5. Brian D
    Replies:
    5
    Views:
    2,810
Loading...

Share This Page