If I have a large string with multiple lines and I want to match part of a line only to end of that line, what is the best way to do that?
So, for example I have something like this and I want it to stop matching when it reaches the new line character.
r"(?P<name>[A-Za-z\s.]+)"
I saw this in a previous answer:
$ – indicates matching to the end of the string, or end of a line if
multiline is enabled.
My question is then how do you “enable multiline” as the author of that answer states?
Simply use
This will match ASCII letters, spaces, tabs or periods. It’ll stop at the first character that’s not included in the group – and newlines aren’t (whereas they are included in
\s, and because of that it’s irrelevant whether multiline mode is turned on or off).