Given a unicode object with the following text:
给定具有以下文本的unicode对象:
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?
实际上,这只会检索最后一行。我能做些什么来获得最后五个?
3 个解决方案
#1
4
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)'
#2
2
Why not just:
为什么不呢:
text[text.index('\n\n') + 2:].splitlines()
# ['aaaa', 'bbbb', 'cccc', 'dddd', 'eeee']
#3
0
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:
如果您搜索到的文本对您不想要的第一部分的字符数有某种限制,为什么不设置只搜索超过X个字母的字词,如:
^[a-z]{2,}
This will get every word bigger than 2 characters.
这将使每个单词大于2个字符。
You can control as:
你可以控制为:
- {3} Exactly 3 occurrences;
- {3}恰好3次出现;
- {6,} At least 6 occurrences;
- {6,}至少发生6次;
- {2,5} 2 to 5 occurrences.
- {2,5}发生2到5次。
#1
4
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)'
#2
2
Why not just:
为什么不呢:
text[text.index('\n\n') + 2:].splitlines()
# ['aaaa', 'bbbb', 'cccc', 'dddd', 'eeee']
#3
0
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:
如果您搜索到的文本对您不想要的第一部分的字符数有某种限制,为什么不设置只搜索超过X个字母的字词,如:
^[a-z]{2,}
This will get every word bigger than 2 characters.
这将使每个单词大于2个字符。
You can control as:
你可以控制为:
- {3} Exactly 3 occurrences;
- {3}恰好3次出现;
- {6,} At least 6 occurrences;
- {6,}至少发生6次;
- {2,5} 2 to 5 occurrences.
- {2,5}发生2到5次。