Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python在正则表达式匹配后捕获5行_Python_Regex - Fatal编程技术网

如何使用python在正则表达式匹配后捕获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'):

我有一个以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

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]
    -三位数字、空格和一个小写字母
  • *
    -行的其余部分
  • ((?:\r?\n.*){0,5})
    -第1组:换行符的零到五次重复,然后是换行符的其余部分
:

输出:

 hell01 
 hell02 
 hell03 
 hell04 
 hell05
请查收。