How lookahead works in conjunction with captured groups?

Viewed 13

My understanding of lookahead syntax is X(?=Y). Here, match X but only if it is followed by Y.

Using the above syntax, I understand the below code:

import re
text = "streets"
pattern = r"t(?=s)"

match = re.findall(pattern, text)
print(match)

Output('t' which comes before 's'): ['t']


However, I am not able to understand the below code.

import re
text = "streets"
pattern = r"(?=(s))"
match = re.findall(pattern, text)
print(match)

Output: ['s', 's']

How lookahead works when there are one or more captured groups inside the lookahead?


I am also not able to understand the below code.

import re
text = "streets"
pattern = r"(?=s)"
match = re.findall(pattern, text)
print(match)

Output: ['', '']


How lookahead works without "X" (?=Y) only?

0 Answers
Related