regex group match

T

Thufir

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
 
T

Thufir

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
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,477
Members
44,898
Latest member
BlairH7607

Latest Threads

Top