RegEx:如何匹配开头和结尾

时间:2016-04-11 18:07:51

标签: regex

我在文件中有一定数量的SIGMETS(航空信息)文本文件,例如:

BGGL SIGMET 3 VALID 281815/282215 BGSF-
BGGL SONDRESTROM FIR SEV TURB FCST AT 1815Z WI N7900 W02550 -
N8200 W01150 - N8130 W00805 - N7540 W01815 - N7540 W02440 - N7900
W02550 SFC/FL080 STNR NC=

ENBD SIGMET C01 VALID 281530/281930 ENVV-
ENOR NORWAY FIR OCNL SEV MTW FCST WI N6200 E00530 - N6300 E00830 -
N6300 E01030 - N6200 E01000 - N6200 E00530 SFC/FL260 STNR NC=

ENSV SIGMET B02 VALID 281500/281900 ENVV-
ENOR NORWAY FIR OCNL SEV TURB FCST WI N5900 E00730 - N5900 E00530 -
N6200 E00530 - N6200 E00730 - N5900 E00730 SFC/FL180 STNR NC=

SIGMET的开头是这样的格式: /([A-Z]{4}) (AIRMET|SIGMET) (\w{1,3}) VALID (\d{6}\/\d{6}) ([A-Z]{4})-/ 并始终以/(WKN|NC|INTSF)=/结尾。

如何构建一个RegEx以匹配每个SIGMET?

提前致谢

P.S。我正在使用JavaScript。

1 个答案:

答案 0 :(得分:0)

import re


with open('a.txt') as file:
    data = '\n\n' + file.read() + '\n\n'

pattern = r'\n\n' + r'([A-Z]{4}) (AIRMET|SIGMET) (\w{1,3}) VALID (\d{6}\/\d{6}) ([A-Z]{4})-.*?(WKN|NC|INTSF)=' + r'\n\n'
pattern_compiled = re.compile(pattern, re.DOTALL)

res = pattern_compiled.finditer(data)

if res:
    for i in res:
        print(i)
        print(i.group())
else:
    print('Not found')