简体   繁体   中英

How to match several lines with regex

Given a unicode object with the following text:

a
b
c
d
e

aaaa
bbbb
cccc
dddd
eeee

I'd like to get the second group of lines, in other words, every line after the blank one. This is the code I've used:

text = ... # the previous text
exp = u'a\nb\nc\nd\n\e\n{2}(.*\n){5}'
matches = re.findall(exp, text, re.U)

This will only retrieve the last line, indeed. What could I do to get the last five ones?

You're repeating the capturing group itself, which overwrites each match with the next repetition.

If you do this

exp = ur'a\nb\nc\nd\n\e\n{2}((?:.*\n){5})'

you get the five lines together.

You can't get to the individual matches unless you spell out the groups manually:

exp = ur'a\nb\nc\nd\n\e\n{2}(.*\n)(.*\n)(.*\n)(.*\n)(.*\n)'

Why not just:

text[text.index('\n\n') + 2:].splitlines()
# ['aaaa', 'bbbb', 'cccc', 'dddd', 'eeee']

if your searched text has some kind of limitation on the number of characters for this first part which you don't want, why not set a search for only words with more than X letters like:

^[a-z]{2,}

This will get every word bigger than 2 characters.

You can control as:

  • {3} Exactly 3 occurrences;
  • {6,} At least 6 occurrences;
  • {2,5} 2 to 5 occurrences.

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