I have this input string
:
string = '''p1 = {
s,
},
p2 = {
{s,
},
p3 = {
s,
},
p2 = {
}s,
},'''
I am trying to replace all paragraphs starting with p2 = {
and ending with },
and end up with this new expected output string
:
p1 = {
s,
},
p3 = {
s,
},
I can achieve this without using a regex by just looping over the lines. But how would I do it with a regex?
This method using re.subn does not make any replacement and I don't understand why not:
import re
repl = ''
pattern = r'p2 = {.*?},'
new_string, number_of_subs_made = re.subn(pattern, repl, string,
re.MULTILINE|re.DOTALL)
This method using re.findall seems to be able to find all matches:
import re
pattern = r'.*?(p2 = {.*?},).*?'
m = re.findall(pattern, string, re.MULTILINE|re.DOTALL)
print('\n'.join(m))
It prints this output:
p2 = {
{s,
},
p2 = {
}s,
},
There is a question on how to emulate grep -v
and exclude lines with pattern matches, and the answer suggests using a negative lookahead: Regular expression to match a line that doesn't contain a word?
And there is a question on how to use negative lookahead across multiple lines here: Regex with negative lookahead across multiple lines
Would it perhaps be possible to somehow invert that negative lookahead and print the lines that do not match?