Skip to content

Instantly share code, notes, and snippets.

@ifyouseewendy
Created July 1, 2015 14:26
Show Gist options
  • Save ifyouseewendy/032d8e249989a2fb0cf7 to your computer and use it in GitHub Desktop.
Save ifyouseewendy/032d8e249989a2fb0cf7 to your computer and use it in GitHub Desktop.

?: - Non capture the group

http://stackoverflow.com/questions/3512471/non-capturing-group

str = "http://stackoverflow.com/questions/tagged/regex"
str =~ /(http|ftp):\/\/([^\/\r\n]+)(\/[^\r\n]*)?/
puts $1 # => http

# with ?:
str = "http://stackoverflow.com/questions/tagged/regex"
str =~ /(?:http|ftp):\/\/([^\/\r\n]+)(\/[^\r\n]*)?/
puts $1 # => stackoverflow.com

*? - ? following a quantifier like * has a different meaning and makes it "ungreedy".

http://stackoverflow.com/questions/13401514/whats-the-profit-of-using

str = "helo world around"
str =~ /(.*)\s/
puts $1 # => helo world

# with ?
str = "helo world around"
str =~ /(.*?)\s/
puts $1 # => helo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment