ruby-1.9.2-p180 :003 > result = "test string".match(/(?<mtch>test)/)
=> #<MatchData "test" mtch:"test">
ruby-1.9.2-p180 :011 > result["mtch"]
=> "test"
ruby-1.9.2-p180 :012 > result["fail"]
IndexError: undefined group name reference: fail
from (irb):12:in `[]'
from (irb):12
from /Users/jeremysmith/.rvm/rubies/ruby-1.9.2-p180/bin/irb:16:in `<main>'
There isn't a MatchData function for checking whether a named group exists. is there some other way to check?
result.names.include? 'mtch'
# => true
result.names.include? 'fail'
# => false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With