且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何提取与文本文件中的正则表达式匹配的行号

更新时间:2021-07-16 00:42:28

这应该可以解决您的问题,假定变量'phrase'中的正则表达式正确

This should solve your problem, presuming you have correct regex in variable 'phrase'

import re

# compile regex
regex = re.compile('[0-9]+')

# open the files
with open('Corpus.txt','r') as inputFile:
    with open('OutputLineNumbers', 'w') as outputLineNumbers:
        # loop through each line in corpus
        for line_i, line in enumerate(inputFile, 1):
            # check if we have a regex match
            if regex.search( line ):
                # if so, write it the output file
                outputLineNumbers.write( "%d\n" % line_i )