Created
October 24, 2012 01:36
-
-
Save robinsloan/3943193 to your computer and use it in GitHub Desktop.
Ruby regex tricks
This file contains hidden or 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
# 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