Skip to content

Instantly share code, notes, and snippets.

@Olgagr
Created July 14, 2013 15:57
Show Gist options
  • Save Olgagr/5994708 to your computer and use it in GitHub Desktop.
Save Olgagr/5994708 to your computer and use it in GitHub Desktop.
Lookahead and Lookbehind Assertions
# Lookahead assertion
str = "123 456. 789"
m = /\d+(?=\.)/.match(str) # m[0] = '456'. Period doesn't count as a part of the match
/\d+(?!\.)/ # negative lookahead
# Lookbehind assertion
str = "David BLACK"
m = /(?<=David )BLACK/ # only match BLACK if it's preceded by 'David'
/(?<!David) BLACK/ # negative lookbehind
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment