I have a string:
s=\"123--abc,123--abc,123--abc\"
I tried using Ruby 1.9\'s new feature \"named groups\" to fetch all named group info:
You can extract the used variables from the regexp using names
method. So what I did is, I used regular scan
method to get the matches, then zipped names and every match to create a Hash
.
class String
def scan2(regexp)
names = regexp.names
scan(regexp).collect do |match|
Hash[names.zip(match)]
end
end
end
Usage:
>> "aaa http://www.google.com.tr aaa https://www.yahoo.com.tr ffffd".scan2 /(?(?https?):\/\/[\S]+)/
=> [{"url"=>"http://www.google.com.tr", "protocol"=>"http"}, {"url"=>"https://www.yahoo.com.tr", "protocol"=>"https"}]