【发布时间】:2011-08-12 08:58:03
【问题描述】:
我正在尝试使用 Python 中的正则表达式匹配有效域名中的标签:
DOMAIN_LABEL_RE = """
\A(
(?<![\d\-]) # cannot start with digit or hyphen, looking behind
([a-zA-Z\d\-]*?)
([a-zA-Z]+)# need at least 1 letter
([a-zA-Z\d\-]*?)
(?!\-) # cannot end with a hyphen, looking ahead
)\Z
"""
我正在尝试使用肯定和否定断言来避免标签开头或末尾出现连字符。
但字符串“-asdf”仍然匹配: e.match(DOMAIN_LABEL_RE, "-asdf", re.VERBOSE).group()
我不明白为什么它仍然匹配。
感谢您的帮助。
M.
【问题讨论】:
-
你已经交换了你的前进和后退。
标签: python regex regex-lookarounds