简体   繁体   中英

match part of a string until it reaches the end of the line (python regex)

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

r"(?P<name>[A-Za-z\t .]+)"

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).

You can enable multiline matching by passing re.MULTILINE as the second argument to re.compile() . However, there is a subtlety to watch out for: since the + quantifier is greedy, this regular expression will match as long a string as possible, so if the next line is made up of letters and whitespace, the regex might match more than one line ( $ matches the end of any string).

There are three solutions to this:

  1. Change your regex so that, instead of matching any whitespace including newline ( \\s ) your repeated character set does not match that newline.
  2. Change the quantifier to +? , the non-greedy ("minimal") version of + , so that it will match as short a string as possible and therefore stop at the first newline.
  3. Change your code to first split the text up into an individual string for each line (using text.split('\\n') .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM