[英]python regex find first word after certain keywords
我有以下python代码,它检索某些关键字后的第一个单词:
file_tokens = ('DATABASE', 'EXTERNAL_FILE', 'FILE', 'FILENAME', 'INCLUDE')
# match newline, only spaces, then exact token, then spaces, then everything but whitespace
search_pattern = r'\n\s*({})\s+([^\s]*)'.format('|'.join(file_tokens))
matches = re.findall(search_pattern, file_content_string, flags=re.IGNORECASE) # find matches
它在以下字符串中工作得很漂亮(包括新行和回车):
# originaly spe1 but with grd ecl file meddled with for nesting
include tests
SIMULATION
SIMULATION_TYPE SUBSURFACE
PROCESS_MODELS
SUBSURFACE_FLOW Flow
MODE BLACK_OIL
OPTIONS
ANALYTICAL_JACOBIAN
ISOTHERMAL
/
/ ! end of subsurface_flow
/ ! end of process models
CHECKPOINT
/
END !! end simulation block
SUBSURFACE
external_file example1.dat
include example2.dat
匹配包含:
matches = [example1.dat,example2.dat]
但是对于像以下仅包含关键字和其他文本的简单字符串而言,它是失败的:
external_file example3.dat
include example4.dat
返回一个空数组或只是最后一项(有点随机):
matches = [example4.dat]或matches = []
任何想法? 谢谢。
UPDATE
好的,修改导入文本后:
external_file example3.dat
include example4.dat
database example5.dat
我已经意识到我的匹配数组只缺少第一项:
matches = [example4.dat,example5.dat]
如何修改正则表达式以包含example3.dat?
我会解决它略有不同。
import re
test1 = """include tests
SIMULATION
SIMULATION_TYPE SUBSURFACE
PROCESS_MODELS
SUBSURFACE_FLOW Flow
MODE BLACK_OIL
OPTIONS
ANALYTICAL_JACOBIAN
ISOTHERMAL
/
/ ! end of subsurface_flow
/ ! end of process models
CHECKPOINT
/A
END !! end simulation block
SUBSURFACE
external_file example1.dat
include example2.dat"""
test2 = """external_file example3.dat
include example4.dat"""
token = re.findall(r'\S+', test1)
token
>>>['include',
'tests',
'SIMULATION',
'SIMULATION_TYPE',
'SUBSURFACE',
'PROCESS_MODELS',
'SUBSURFACE_FLOW',
'Flow',
'MODE',
'BLACK_OIL',
'OPTIONS',
'ANALYTICAL_JACOBIAN',
'ISOTHERMAL',
'/',
'/',
'!',
'end',
'of',
'subsurface_flow',
'/',
'!',
'end',
'of',
'process',
'models',
'CHECKPOINT',
'/',
'END',
'!!',
'end',
'simulation',
'block',
'SUBSURFACE',
'external_file',
'example1.dat',
'include',
'example2.dat']
当你对你的话语进行了标记时,我会构建二元语法
bi_grams = [(a,b) for a,b in zip(token[:-1], token[1:]) ]
然后过滤那些包含文件标记的二元组作为第一个条目
file_tokens = ('DATABASE', 'EXTERNAL_FILE', 'FILE', 'FILENAME', 'INCLUDE')
bi_grams_of_interest = [bi_gram for bi_gram in bi_grams if bi_gram[0].upper() in file_tokens]
bi_grams_of_interest
>>>[('include', 'tests'),
('external_file', 'example1.dat'),
('include', 'example2.dat')]
如果你为test2运行它我得到以下输出
>>>[('external_file', 'example3.dat'), ('include', 'example4.dat')]
你需要用^
替换\\n
并将re.M
添加到标志:
r'(?mi)^\s*(?:{})\s+(\S+)'.format('|'.join(file_tokens))
现在, ^\\s*
将匹配一行的开头,然后匹配0或更多的空格。
查看Python演示 :
import re
file_content_string="""external_file example3.dat
include example4.dat
database example5.dat"""
file_tokens = ('DATABASE', 'EXTERNAL_FILE', 'FILE', 'FILENAME', 'INCLUDE')
search_pattern = r'^\s*(?:{})\s+(\S+)'.format('|'.join(file_tokens))
matches = re.findall(search_pattern, file_content_string, flags=re.I|re.M)
print(matches)
输出: ['example3.dat', 'example4.dat', 'example5.dat']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.