Today I Learned

Extracting substring using regexp

If you want to grab some part of the string using a regular expression there is a high chance that you are using match or scan method

pry(main)> '$$$ Tony Stark $$$'.scan(/\w+ \w+/)[0] # -> Tony Stark

pry(main)> '$$$ Tony Stark $$$'.match(/(\w+ \w+)/)[1] # -> Tony Stark

next time try this

pry(main)> '$$$ Tony Stark $$$'[/(\w+ \w+)/] # -> Tony Stark

More advanced examples

pry(main)> '$$$ Tony Stark $$$'[/\${3}\s(\w+\s\w+)\s\${3}/, 1] # -> Tony Stark

pry(main)> '$$$ Tony Stark $$$'[/\${3}\s(?<name>\w+\s\w+)\s\${3}/, :name] # -> Tony Stark

as always visit www.crystular.org to validate your regexp