Skip to content

String#scan

August 7, 2012

How do you test for multiple regexp matches for a single string

I came across String#scan


1.9.3p194 :131 > v
 => "\nMatthew \npassage\nMatthew 1\n40001001-40001025125\nword-search5\n" 
1.9.3p194 :132 > v.scan(/.*/)
 => ["25"] 
1.9.3p194 :133 > Regexp.last_match
 => #<MatchData "25"> 
1.9.3p194 :134 > v.scan(/(.*)/)
 => [["25"]] 
1.9.3p194 :135 > Regexp.last_match
 => #<MatchData "25" 1:"25"> 


If you want to use a variable
1.9.3p194 :136 > v.scan(/(?.*)/)
 => [["25"]] 
1.9.3p194 :137 > Regexp.last_match
 => #<MatchData "25" verse_count:"25"> 
1.9.3p194 :138 > Regexp.last_match[:verse_count]
 => "25"

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: