首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在regex verbose中使用模式

在regex verbose中使用模式
EN

Stack Overflow用户
提问于 2019-02-01 04:55:52
回答 1查看 52关注 0票数 1

尝试使用re.VERBOSE获取输出

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

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

原创

代码语言:javascript
复制
    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})
    """

变化

代码语言:javascript
复制
    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)

它返回不应该返回的none

EN

回答 1

Stack Overflow用户

发布于 2019-02-01 06:51:07

代码语言:javascript
复制
^
(?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?

这是可行的

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54469097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档