189. Advanced Regular Expressions with re

1. Positive Lookahead

Copy

import re

# Match 'Python' only if followed by 'Rocks'
pattern = r"Python(?= Rocks)"
text = "Python Rocks is amazing!"
matches = re.findall(pattern, text)
print(matches)  # Output: ['Python']

2. Negative Lookahead

Copy

import re

# Match 'Python' only if not followed by 'Rocks'
pattern = r"Python(?! Rocks)"
text = "Python is fun, but Python Rocks too!"
matches = re.findall(pattern, text)
print(matches)  # Output: ['Python']

3. Positive Lookbehind

Copy


4. Negative Lookbehind

Copy


5. Named Groups

Copy


6. Flags: Ignore Case

Copy


7. Flags: Multiline Mode

Copy


8. Non-Capturing Groups

Copy


9. Match with Unicode Characters

Copy


10. Combining Multiple Flags

Copy


Last updated