Python检查是否存在行(两个关键字)忽略之间的空格

时间:2017-12-01 15:17:05

标签: python regex

我的示例文件有一个结构:

Library      Collections
Library      XML    use_lxml=True
Library      ute_wtssim
Library      libraries.ta_infomodel
Library      ute_tshark
Library      PDMLChecker.py
Library    libraries.ta_ue_configuration
Library      libraries.ta_tshark
Library    libraries.ta_file_system
Resource     environment_setup.robot
Variables    ../tshark_filters.py
Variables    ../global_variables.py
Variables    tracing_tshark_filters.py

我想在文件行中找到:Library libraries.ta_infomodel忽略空格(列宽,不是常数)。

你可以给我建议吗?

编辑: 我想检查线是否存在... 包含两个关键字的行,忽略之间的空格。

3 个答案:

答案 0 :(得分:1)

只需使用\s*即可。 \s代表空白。 *表示任意数量(包括零):

import re
s = '''Library      Collections
Library      XML    use_lxml=True
Library      ute_wtssim
Library      libraries.ta_infomodel
Library      ute_tshark
Library      PDMLChecker.py
Library      resources.DevWro1.pdml_validation.PdmlValidation
Library    libraries.ta_ue_configuration
Library      libraries.ta_tshark
Library    libraries.ta_file_system
Resource     environment_setup.robot
Variables    ../tshark_filters.py
Variables    ../global_variables.py
Variables    tracing_tshark_filters.py'''

matches = re.findall('Library\s*libraries\.ta_infomodel', s)

for match in matches:
    print(match)

答案 1 :(得分:0)

试试这个:

content = """
Library      Collections
Library      XML    use_lxml=True
Library      ute_wtssim
Library      libraries.ta_infomodel
Library      ute_tshark
Library      PDMLChecker.py
Library      resources.DevWro1.pdml_validation.PdmlValidation
Library    libraries.ta_ue_configuration
Library      libraries.ta_tshark
Library    libraries.ta_file_system
Resource     environment_setup.robot
Variables    ../tshark_filters.py
Variables    ../global_variables.py
Variables    tracing_tshark_filters.py
"""

import re
regex = re.compile(r'Library\s+libraries\.ta_infomodel')
line = regex.search(content)

一些解释。为了通过正则表达式找到一行,您应该使用re.search,对于许多re.findall

\s+ - 适用于多个空格。

\. - 为点。使用必须添加斜杠,因为只是dot表示任何字符。

答案 2 :(得分:-1)

也可以用列表理解来完成

import re
lines = open(fname, 'r').readlines()
found = [s for s in lines if re.match(".* Library\s+libraries\.ta_infomodel.*", s)]