Adam
Adam

Reputation: 3138

Matching new lines

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.

Upvotes: 1

Views: 77

Answers (1)

Andrew Clark
Andrew Clark

Reputation: 208665

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 \n in your regex:

pattern = re.compile(r'^HESAID:$|^SHESAID:$', re.MULTILINE)

The re.MULTILINE flag 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 the S makes it optional:

pattern = re.compile(r'^S?HESAID:$', re.MULTILINE)

Upvotes: 4

Related Questions