Skip to content

Instantly share code, notes, and snippets.

@ryancastro
Last active August 29, 2015 14:14
Show Gist options
  • Save ryancastro/89824fdc48c2e52cca6f to your computer and use it in GitHub Desktop.
Save ryancastro/89824fdc48c2e52cca6f to your computer and use it in GitHub Desktop.
Easy Ruby Regex - the shortest regex find, and replace methods in Ruby.
#index capture groups:
url = "https://gist.github.com/ryancastro/whatever.git"
p url[/(.*)\/whatever.git/, 1]
p url.gsub(/(.*)\/whatever.git/, '\1')
#named capture groups:
url = "https://gist.github.com/ryancastro/whatever.git"
p url[/(?<url>.*)\/whatever.git/, "url"]
p url.gsub(/(?<url>.*)\/whatever.git/, '\k<url>')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment