Created
July 14, 2013 15:57
-
-
Save Olgagr/5994708 to your computer and use it in GitHub Desktop.
Lookahead and Lookbehind Assertions
This file contains 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
# 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