请分析python中的正则表达式

时间:2014-05-15 14:08:34

标签: python regex

我已经阅读了一些关于正则表达式的文档,但我无法分析这个正则表达式。 我理解^.+创建了一些任何字符,然后\\@@个字符。链的其余部分我无法解释清楚。

^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$

1 个答案:

答案 0 :(得分:3)

使用Regex 101。该网站将显示正则表达式中每个元素的解释。您需要首先将\\替换为\,即使用:

^.+\@(\[?)[a-zA-Z0-9\\-\\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$

解释是:

^ assert position at start of the string
.+ matches any character (except newline)
    Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\@ matches the character @ literally
1st Capturing group (\[?)
    \[? matches the character [ literally
        Quantifier: Between zero and one time, as many times as possible, giving back as needed [greedy]
[a-zA-Z0-9\\-\\.]+ match a single character present in the list below
    Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    a-z a single character in the range between a and z (case sensitive)
    A-Z a single character in the range between A and Z (case sensitive)
    0-9 a single character in the range between 0 and 9
    \\-\\ a single character in the range between the following two characters
        \\ the literal character \
        \\ the literal character \
    . the literal character .
\. matches the character . literally
2nd Capturing group ([a-zA-Z]{2,3}|[0-9]{1,3})
    1st Alternative: [a-zA-Z]{2,3}
        [a-zA-Z]{2,3} match a single character present in the list below
            Quantifier: Between 2 and 3 times, as many times as possible, giving back as needed [greedy]
            a-z a single character in the range between a and z (case sensitive)
            A-Z a single character in the range between A and Z (case sensitive)
    2nd Alternative: [0-9]{1,3}
        [0-9]{1,3} match a single character present in the list below
            Quantifier: Between 1 and 3 times, as many times as possible, giving back as needed [greedy]
            0-9 a single character in the range between 0 and 9
3rd Capturing group (\]?)
    \]? matches the character ] literally
        Quantifier: Between zero and one time, as many times as possible, giving back as needed [greedy]
$ assert position at end of the string