Skip to content

Instantly share code, notes, and snippets.

@robinsloan
Created October 24, 2012 01:36
Show Gist options
  • Save robinsloan/3943193 to your computer and use it in GitHub Desktop.
Save robinsloan/3943193 to your computer and use it in GitHub Desktop.
Ruby regex tricks
# variables from regex
# must be literal; must be on left side of comparison
if /Robin is (?<adjective>\w+)/ =~ "Robin is awesome" then
puts "#{adjective}"
end
# indexing into a string via regex (whoa)
str = "Robin is awesome"
adjective = str[/Robin is (?:\w+)/, 1]
# or
adjective = str[/Robin is (?<adjective>\w+)/, :adjective]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment