Skip to content

Instantly share code, notes, and snippets.

@wbailey
Created March 10, 2011 21:08
Show Gist options
  • Save wbailey/864933 to your computer and use it in GitHub Desktop.
Save wbailey/864933 to your computer and use it in GitHub Desktop.
Using a back reference in the regular expression definition
ruby-1.9.2-p136 > 'asdf asdf--asdf asdf-asdf'.gsub(/( |-)( |-)?/, '_')
=> "asdf_asdf_asdf_asdf_asdf"
ruby-1.9.2-p136 > 'asdf asdf--asdf asdf-asdf'.gsub(/( |-)\1?/, '_')
=> "asdf_asdf_asdf_asdf_asdf"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment