正则表达式的Python条件

时间:2018-09-18 14:15:55

标签: python regex

我有一个包含这些数据的文件:

PAS_BEGIN_0009999
    T71_MANUFACTURER_4=98
    T71_COLOR_ID_7=000
    T71_OS_7=08
PAS_END_0009999

PAS_BEGIN_0009996
    T72_VAS_SERVICE_IDENTIFIER_6=
    T72_ORDER_NB_7=0003
    T72_TECHNOLOGY_7=01
PAS_END_0009996

TPV_BEGIN
    PAS_20819001=3600000 
TPV_END

如何简单地在PAS_BEGIN_0009996PAS_BEGIN_0009996之间隔离内容

以下是示例的链接:https://regexr.com/3vmeq

它找到了东西,但是我的python代码没找到任何东西。

if re.match("PAS_BEGIN_0009999([\S\s]*)PAS_END_0009999", line):
    data.append(line)
    print(line)

有人可以帮我吗?谢谢

1 个答案:

答案 0 :(得分:1)

You are reading a text file line by line, but your expected match is located on several lines. You need to read the whole file into a variable, then run a regex like yours, or, better, a pattern like a.*?b with re.DOTALL option so that . could match line break chars.

So, you may use something like

import re
fpath = 'your_file_path.txt'
data = ''
pattern=r'PAS_BEGIN_0009999(.*?)PAS_END_0009999'
with open(filepath, "r") as f:
    contents = f.read()
    m = re.search(pattern, contents)
    if m:
        data = m.group(1) # or `.group() if you need to include PAS_BEGIN_0009999 and PAS_END_0009999

If you need to find multiple occurrences, replace the re.search part (all lines after contents) with

data = re.findall(pattern, contents)

See the regex demo