在正则表达式中使用模式

时间:2019-01-31 20:55:52

标签: regex python-3.x

尝试使用re.VERBOSE获取输出

我正在尝试更改模式代码,如图所示

我尝试了一些代码,但未显示任何内容,因为它未返回任何内容

原始

    taf_header_pattern = """
        ^
        (TAF)?    # TAF header (at times missing or duplicate)
        \s*
        (?P<type> (COR|AMD|AMD\sCOR|COR\sAMD|RTD)){0,1}
        \s* # There may or may not be space as COR/AMD/RTD is optional
        (?P<icao_code> [A-Z]{4}) # Station ICAO code
        \s* # at some aerodromes does not appear
        (?P<origin_date> \d{0,2}) # at some aerodromes does not appear
        (?P<origin_hours> \d{0,2}) # at some aerodromes does not appear
        (?P<origin_minutes> \d{0,2}) # at some aerodromes does not appear
        Z? # Zulu time (UTC, that is) # at some aerodromes does not appear
        \s*
        (?P<valid_from_date> \d{0,2})
        (?P<valid_from_hours> \d{0,2})
        /
        (?P<valid_till_date> \d{0,2})
        (?P<valid_till_hours> \d{0,2})
    """

已更改

    taf_header_pattern = """
        ^
        (?P<icao_code> [A-Z]{4}) # Station ICAO code
        \s*
        (?P<fix_code> (FIXED BASE)) # FIXED BASE
        \s*
        (?P<type> (COR|AMD|AMD\sCOR|COR\sAMD|RTD)){0,1}
        \s* # There may or may not be space as COR/AMD/RTD is optional
        (?P<origin_date> \d{0,2}) # at some aerodromes does not appear
        (?P<origin_hours> \d{0,2}) # at some aerodromes does not appear
        (?P<origin_minutes> \d{0,2}) # at some aerodromes does not appear
        Z? # Zulu time (UTC, that is) # at some aerodromes does not appear
        \s*
        (?P<valid_from_date> \d{0,2})
        (?P<valid_from_hours> \d{0,2})
        /
        (?P<valid_till_date> \d{0,2})
        (?P<valid_till_hours> \d{0,2})
    """

test_string00 = "KIND FIXED BASE 3117Z-0117Z"
test_string01 = "KGEG FIXED BASE AMD02 3119Z-0110Z 311958Z"
test_string02 = "KGEG FIXED BASE AMD02 COR01 3119Z-0110Z 311958Z"

header_taf = re.match(taf_header_pattern, string, re.VERBOSE)

print(header_taf)

它不返回不应执行的操作

1 个答案:

答案 0 :(得分:0)

^
(?P<icao_code> [A-Z]{4}) # Station ICAO code
\s*
(?P<fix_code> (FIXED\ BASE)) # FIXED BASE
\s*
(?P<type>(AMD\d{2}\sCOR\d{2}|COR\d{2}|AMD\d{2})){0,1}
\s* # There may or may not be space as COR/AMD/RTD is optional
(?P<valid_from_date>\d{0,2})
(?P<valid_from_hours>\d{0,2}) 
Z?
\-?
(?P<valid_till_date>\d{0,2})
(?P<valid_till_hours>\d{0,2})  
Z?
\s* # There may or may not be space as COR/AMD/RTD is optional
(?P<origin_date>\d{0,2}) # at some aerodromes does not appear
(?P<origin_hours>\d{0,2}) # at some aerodromes does not appear
(?P<origin_minutes>\d{0,2}) # at some aerodromes does not appear
Z?

这有效