I want to turn all occurrences of A...A into B...B for some filler between the two A's. The filler must be allowed to contain new line characters. I assumed re.DOTALL
was the solution.
Here's a python script:
import re
tt1 = re.sub(r'A(?P<text>.*)A','B\g<text>B','AhiA')
print tt1
tt1 = re.sub(r'A(?P<text>.*)A','B\g<text>B','A\nhiA')
print tt1
tt1 = re.sub(r'A(?P<text>[.]*)A','B\g<text>B','A\nhiA')
print tt1
tt1 = re.sub(r'A(?P<text>.*)A','B\g<text>B','A\nhiA',re.DOTALL)
print tt1
And here's the output:
BhiB
A
hiA
A
hiA
A
hiA
What gives, and how can I replace 'A\nhiA' with 'B\nhiB'?
To match the new line regex in Python, use the pattern \n. On Linux OS, it is \n; on Windows, the line break matches with \r\n, and in the old version of Mac, it is \r.
By using re. DOTALL flag, you can modify the behavior of dot (.) character to match the newline character apart from other characters.
\n. Matches a newline character. \r. Matches a carriage return character.
The fourth parameter to re.sub()
is count
(the maximum number of replace operations to be performed). re.DOTALL
is 16
, so you're passing a (valid) parameter in an unexpected place.
Use
re.sub(r'A(?P<text>.*)A','B\g<text>B','A\nhiA', flags=re.DOTALL)
(or place re.DOTALL
in position five):
re.sub(r'A(?P<text>.*)A','B\g<text>B','A\nhiA', 0, re.DOTALL)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With