如何使用python在正则表达式匹配后捕获5行
我有一个以3位代码开头的文本我写了一个逻辑来捕获当前行,但我需要连续捕获接下来的5行如何使用python在正则表达式匹配后捕获5行,python,regex,Python,Regex,我有一个以3位代码开头的文本我写了一个逻辑来捕获当前行,但我需要连续捕获接下来的5行 import re newtxt="200 sample text with many lines\n hell01 \n hell02 \n hell03 \n hell04 \n hell05\n hell06\n hell07 \n hell08" text = re.compile(r'^\d{3} [a-z].*') for line in newtxt.split('\n'):
import re
newtxt="200 sample text with many lines\n hell01 \n hell02 \n hell03 \n hell04 \n hell05\n hell06\n hell07 \n hell08"
text = re.compile(r'^\d{3} [a-z].*')
for line in newtxt.split('\n'):
if text.match(line):
print(line)
使用
iter
Ex:
import re
newtxt="200 sample text with many lines\n hell01 \n hell02 \n hell03 \n hell04 \n hell05\n hell06\n hell07 \n hell08"
text = re.compile(r'^\d{3} [a-z].*')
newtext = iter(newtxt.splitlines())
for line in newtext:
if text.match(line):
for _ in range(5):
print(next(newtext))
hell01
hell02
hell03
hell04
hell05
text = re.compile(r'^\d{3} [a-z].*')
with open(filename) as infile:
for line in infile:
if text.match(line):
for _ in range(5):
print(next(infile))
输出:
import re
newtxt="200 sample text with many lines\n hell01 \n hell02 \n hell03 \n hell04 \n hell05\n hell06\n hell07 \n hell08"
text = re.compile(r'^\d{3} [a-z].*')
newtext = iter(newtxt.splitlines())
for line in newtext:
if text.match(line):
for _ in range(5):
print(next(newtext))
hell01
hell02
hell03
hell04
hell05
text = re.compile(r'^\d{3} [a-z].*')
with open(filename) as infile:
for line in infile:
if text.match(line):
for _ in range(5):
print(next(infile))
如果您是从文件对象读取此文件,则不需要
iter
方法。您可以直接迭代这些行
Ex:
import re
newtxt="200 sample text with many lines\n hell01 \n hell02 \n hell03 \n hell04 \n hell05\n hell06\n hell07 \n hell08"
text = re.compile(r'^\d{3} [a-z].*')
newtext = iter(newtxt.splitlines())
for line in newtext:
if text.match(line):
for _ in range(5):
print(next(newtext))
hell01
hell02
hell03
hell04
hell05
text = re.compile(r'^\d{3} [a-z].*')
with open(filename) as infile:
for line in infile:
if text.match(line):
for _ in range(5):
print(next(infile))
你可以用
r'(?m)^\d{3} [a-z].*((?:\r?\n.*){0,5})'
看。注:代码中的(?m)
可替换为re.m
标志
详细信息
-行的开头^
-三位数字、空格和一个小写字母\d{3}[a-z]
-行的其余部分*
-第1组:换行符的零到五次重复,然后是换行符的其余部分((?:\r?\n.*){0,5})
hell01
hell02
hell03
hell04
hell05
请查收。