I have the following regexp:
pattern = re.compile(r"HESAID:|SHESAID:")
It’s working correctly. I use it to split by multiple delimiters like this:
result = pattern.split(content)
What I want to add is verification so that the split does NOT happend unless HESAID: or SHESAID: are placed on new lines. This is not working:
pattern = re.compile(r"\nHESAID:\n|\nSHESAID:\n")
Please help.
It would be helpful if you elaborated on how exactly it is not working, but I am guessing that the issue is that it does not match consecutive lines of HESAID/SHESAID. You can fix this by using beginning and end of line anchors instead of actually putting
\nin your regex:The
re.MULTILINEflag is necessary so that^and$match at beginning and end of lines, instead of just the beginning and end of the string.I would probably rewrite the regex as follows, the
?after theSmakes it optional: