Created
April 7, 2014 07:55
-
-
Save billhorsman/10016311 to your computer and use it in GitHub Desktop.
Captured Groups in Regex and Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I didn't know that this test made the captured variable available as local variables. | |
if /(?<x>\d+)/ =~ "foo 123 bar" | |
puts x | |
end | |
# => 123 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ruby 2.1's Regexp class says:
That's useful!
Note, this isn't new. It's been around since at least Ruby 1.9.3.