JavaScript
x
9
1
import re
2
3
text = """
4
Tumble Trouble Twwixt Two Towns!
5
Was the Moon soon in the Sea
6
Or soon in the sky?
7
Nobody really knows YET.
8
"""
9
How should I make the match happen only when the occurence is found twice in a line?
Regular expression that highlights two ‘o’s that appear beside each other only if there is another occurence of two ‘o’s appearing beside each other subsequently in the same line
Advertisement
Answer
You can match a single word char with a backreference, and group that again.
The word character will become group 2 as the groups are nested, then the outer group will be group 1.
Then you can assert group 1 using a positive lookahead again in the line.
JavaScript
1
2
1
((w+)2)(?=.*?1)
2
The pattern matches:
(
Capture group 1(w+)2
Match 1+ word chars in capture group 2 followed by a backreference to group 2 to match the same again
)
Close group 1(?=.*?1)
Positive lookahead to assert the captured value of group 1 in the line
See a regex demo and a Python demo.
Example
JavaScript
1
2
1
print(re.compile(r"((w+)2)(?=.*?1)").sub('{g<1>}', text.rstrip()))
2
Output
JavaScript
1
5
1
Tumble Trouble Twwixt Two Towns!
2
Was the M{oo}n soon in the Sea
3
Or soon in the sky?
4
Nobody really knows YET.
5