- Python Natural Language Processing
- Jalaj Thanaki
- 100字
- 2021-07-15 17:02:01
Negative lookahead
Negative lookahead matches the string which is definitely not followed by the pattern which we have defined in the regex pattern part.
Let's give an example to understand the negative lookahead:
- Consider the sentence: I play on the playground. It is the best ground.
- Now you want to extract play only if it is not followed by the string ground. In this case, you can use negative lookahead.
The syntax of negative lookahead is (?!pattern)
The regex r play(?!ground) matches play, but only if it is not followed by ground. Thus the play just before on is matched.