Basic Python Regular expression: re.DOTALL *not* matching new lines
python-2.7, regex
Solution
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)
Problem
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'?