Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created October 30, 2010 17:23
Show Gist options
  • Save ahoward/655544 to your computer and use it in GitHub Desktop.
Save ahoward/655544 to your computer and use it in GitHub Desktop.
re = / ^ (foo) (bar) $ /iox
match, foo, bar = re.match('FOOBAR').to_a
if match
p :foo => foo, :bar => bar #=> {:foo=>"FOO", :bar=>"BAR"}
end
@JEG2
Copy link

JEG2 commented Oct 30, 2010

Or:

if / \A (?<foo>foo) (?<bar>bar) /iox =~ "FOOBAR"
  p foo: foo, bar: bar
end
# $ ruby -v re.rb 
# ruby 1.9.2dev (2010-04-28 trunk 27536) [x86_64-darwin10.3.0]
# {:foo=>"FOO", :bar=>"BAR"}

James Edward Gray II

@raggi
Copy link

raggi commented Oct 30, 2010

#captures # ftw

I really just wish [] accepted more than one group index.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment