1

我正在尝试使用 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。

4

1 回答 1

3

\A匹配字符串的开头,如果该位置之前没有连字符,则后面的查找匹配。

你在字符串的开头,当然前面没有字符!

改用负前瞻:(?![\d\-]).

类似于字符串的结尾。您必须改为使用否定的lookbehind (?<!\-)

我认为与您当前的等效表达式是:

DOMAIN_LABEL_RE = """
(?i               # case insensitive
  \A(
    ([a-z])       # need at least 1 letter and cannot start with digit or hyphen
    ([a-z\d-]*?)
    (?<!-)        # cannot end with a hyphen
  )\Z
)
"""

注意:我没有检查表达式是否真的适合您要解决的问题。

于 2011-08-12T09:04:19.013 回答