-
-
Save hardbap/aebeedcccdc46c8314dfcf33159b87e5 to your computer and use it in GitHub Desktop.
Ruby 1.9 regex, named capture groups to local variables
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
/(?<area_code>\d{3}) (?<local_number>\d{3}-\d{4})/ =~ "123 456-7890" | |
local_variables # => [:area_code, :local_number] | |
area_code # => "123" | |
local_number # => "456-7890" |
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
regex = /(?<area_code>\d{3}) (?<local_number>\d{3}-\d{4})/ | |
regex =~ "123 456-7890" | |
local_variables # => [:regex] | |
# also doesn't work if you switch string and regex in eval | |
"123 456-7890" =~ /(?<area_code>\d{3}) (?<local_number>\d{3}-\d{4})/ | |
local_variables # => [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment