Python正则表达式在某些关键字之后找到第一个单词

时间:2019-03-21 13:33:54

标签: python regex

我有以下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]或matchs = []

有什么主意吗?谢谢。

更新

确定,修改导入文本后:

external_file example3.dat

include example4.dat

database example5.dat

我意识到我的matchs数组仅缺少第一项:

  

matches = [example4.dat,example5.dat]

如何修改正则表达式以包含example3.dat?

2 个答案:

答案 0 :(得分:0)

我会解决一些不同的问题。

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')]

答案 1 :(得分:0)

您需要将\n替换为^,并将re.M添加到标志:

r'(?mi)^\s*(?:{})\s+(\S+)'.format('|'.join(file_tokens))

现在,^\s*将匹配行的开头,然后匹配0个或多个空格。

请参见Python demo

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']