簡體   English   中英

Python正則表達式分組?

[英]Python regular expression grouping?

我一直在嘗試尋找與項目電子郵件中的正則表達式匹配的代碼。 這些是要求:

電子郵件必須采用acct @ domain的形式

  • acct是1個或多個字符,並且僅由大寫或小寫字母字符,數字字符,破折號,句點,下划線和連字符組成
  • acct不能以下划線,破折號,句號或連字符開頭或結尾。 每個期間前后必須至少有兩個字母。
  • 域是5個或更多字符,並且僅由大寫或小寫字母字符,數字字符,破折號,句點和連字符,下划線組成
  • 域必須至少包含一個句點,並且不能以下划線,破折號,句點或連字符開頭或結尾。 每個期間前后必須至少有兩個字母。
  • 我已經用代碼找出了acct部分:

    if re.search("^[a-zA-z0-9]+[a-zA-z0-9-_]*$|^[a-zA-z0-9]+[a-zA-z0-9-_]+[\.]{1}[a-zA-z0-9]{2,}$", email):
        print "valid!"
    

    也是域:

    if re.search("^[a-zA-z0-9]+[a-zA-z0-9-_]+[\.]{1}[a-zA-z0-9]{2,}$", email):
        print "valid!"
    

    我的問題是我無法弄清楚如何將它們組合在一起並放置@符號

    我已經嘗試了以下方法,但是似乎沒有用。

    if re.search("(^[a-zA-z0-9]+[a-zA-z0-9-_]*$|^[a-zA-z0-9]+[a-zA-z0-9-_]+[\.]{1}[a-zA-z0-9]{2,}$)@(^[a-zA-z0-9]+[a-zA-z0-9-_]+[\.]{1}[a-zA-z0-9]{2,}$)", email):<br>
        print "valid!
    

    它不起作用! 我無法與之匹敵。 如果您有減少代碼的建議,請告訴我!

    擺脫兩組的錨點,並將其應用於整個組

    if re.search(r"^(?:[a-zA-Z0-9]+[a-zA-Z0-9-]*|[a-zA-Z0-9]+[a-zA-Z0-9-]+\.[a-zA-Z0-9]{2,})@[a-zA-Z0-9]+[-\w]+\.[a-zA-Z0-9]{2,}$", email):
        print "valid!"
    

    所做的更改

    • ^$應用於整個正則表達式

    • [\\.]{1}可以簡化為\\. 因為它只匹配的一次出現.

    • [a-zA-z0-9-_]可以簡化為[-\\w]

    使用非捕獲組來組合兩個正則表達式。

    if re.search(r"^(?:[a-zA-Z0-9]+[a-zA-Z0-9-]*|[a-zA-Z0-9]+[a-zA-Z0-9-]+[.][a-zA-Z0-9]{2,})@[a-zA-Z0-9]+[a-zA-Z0-9-_]+[.][a-zA-Z0-9]{2,}$", email):
        print "valid"
    

    演示

    正則表達式:

    ^                        the beginning of the string
    (?:                      group, but do not capture:
      [a-zA-Z0-9]+             any character of: 'a' to 'z', 'A' to
                               'Z', '0' to '9' (1 or more times)
      [a-zA-Z0-9-]*            any character of: 'a' to 'z', 'A' to
                               'Z', '0' to '9', '-' (0 or more times)
     |                        OR
      [a-zA-Z0-9]+             any character of: 'a' to 'z', 'A' to
                               'Z', '0' to '9' (1 or more times)
      [a-zA-Z0-9-]+            any character of: 'a' to 'z', 'A' to
                               'Z', '0' to '9', '-' (1 or more times)
      [.]                      any character of: '.'
      [a-zA-Z0-9]{2,}          any character of: 'a' to 'z', 'A' to
                               'Z', '0' to '9' (at least 2 times)
    )                        end of grouping
    @                        '@'
    [a-zA-Z0-9]+             any character of: 'a' to 'z', 'A' to 'Z',
                             '0' to '9' (1 or more times)
    [a-zA-Z0-9-_]+           any character of: 'a' to 'z', 'A' to 'Z',
                             '0' to '9', '-', '_' (1 or more times)
    [.]                      any character of: '.'
    [a-zA-Z0-9]{2,}          any character of: 'a' to 'z', 'A' to 'Z',
                             '0' to '9' (at least 2 times)
    $                        before an optional \n, and the end of the
                             string
    

    下面是可以驗證您所有條件的正則表達式,我希望它更有效。

    ^(?![\W_])((?:([\w-]{2,})\.?){1,})(?<![\W_])@(?![\W_])(?=[\w.-]{5,})(?=.+\..+)((?:([\w-]{2,})\.?){1,})(?<![\W_])$
    

    這是正則表達式演示

    暫無
    暫無

    聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

     
    粵ICP備18138465號  © 2020-2024 STACKOOM.COM