[英]How to capture 5 lines after Regex match using python
我有一个以 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'):
if text.match(line):
print(line)
使用iter
器
前任:
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))
Output:
hell01
hell02
hell03
hell04
hell05
如果您从文件 object 中读取此内容,则不需要iter
方法。 您可以直接迭代行。
前任:
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} [az]
- 三位数字、空格和一个小写字母.*
- 线的rest((?:\r?\n.*){0,5})
- 第 1 组:重复 0 到 5 次换行符,然后是行的 rest。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"
pattern = re.compile(r'^\d{3} [a-z].*((?:\r?\n.*){0,5})', re.M)
m = pattern.search(newtxt)
if m:
print( m.group(1) )
Output:
hell01
hell02
hell03
hell04
hell05
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.