简体   繁体   English

正则表达式电子邮件地址验证

[英]Regex email address validation

Can someone please explain this java Regex to me? 有人可以向我解释这个Java Regex吗?

^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|in|aero|jobs|museum)$

This regex is used to validate an email address . 此正则表达式用于验证电子邮件地址

Validating email addresses is now considered bad practice ( stop validating email addresses with regex ), especially with such expression as in your question. 如今,验证电子邮件地址被认为是一种不好的做法( 停止使用regex验证电子邮件地址 ),尤其是使用问题中的表达式时。 For example here's a more complete expression . 例如,这是一个更完整的表达式

As for this expression let's break it in parts: 至于此表达式,我们将其分为几部分:

Beginning of the matched string 匹配字符串的开头

^

Matches at least one character from the list 匹配列表中的至少一个字符

[a-z0-9!#$%&'*+/=?^_`{|}~-]+

Non-capturing ( see backreference ) group which can be repeated 0..n times, that matches a . 可以重复0..n次的非捕获( 请参阅反向引用 )组,该组与匹配. and then at least one character from the list. 然后从列表中至少选择一个字符。

(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*

Just this character 只是这个角色

@

Non-capturing group matching one character in this list [a-z0-9] and then possibly more characters from the following lists. 非捕获组匹配此列表中的一个字符[a-z0-9] ,然后匹配以下列表中的更多字符。 Matched string must start and end with [a-z0-9] and inside it can have [a-z0-9-]. 匹配的字符串必须以[a-z0-9-]开头和结尾,并且在其内部可以具有[a-z0-9-]。

(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+

Non-capturing group that matches 2 uppercase letters or one of the words. 匹配2个大写字母或一个单词的非捕获组。

(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|in|aero|jobs|museum)

End of the string. 字符串的结尾。

$
^                                       # Beginning of the line
[a-z0-9!#$%&'*+/=?^_`{|}~-]+            # One or more (+) characters from the 
                                        bracket expression, i.e., letters [a-z],
                                        numbers [0-9], !, $, %, et cetera
(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*    # Zero or more (*) of the above
                                        expression, preceded by a dot \\.
@                                       # Literal @
(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+ # A digit or a letter, followed by 
                                        optional digits, letters, or dashes, 
                                        followed by a a dot
(?:[A-Z]{2}|com|org|net...)             # Country code ([A-Z]{2}), or a top level
                                        domain, such as com, org, net.
$                                       # End of the line

Using a concrete example, john@foo.com . 使用具体示例john@foo.com The first part of the e-mail, john , will be matched by ^[a-z0-9!#$%&'*+/=?^_{|}~-]+ . 电子邮件的第一部分john将与^[a-z0-9!#$%&'*+/=?^_{|}~-]+匹配。 The @ will be matched by, well, @ . @会,不错,匹配@ The domain foo , as well as the dot, is matched by (?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\\\.)+ . foo和点由(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\\\.)+匹配。 Finally, the TLD com is matched by the alternation (?:[AZ]{2}|com|org|net|gov|mil|biz|info|mobi|name|in|aero|jobs|museum) . 最后,TLD com通过交替匹配(?:[AZ]{2}|com|org|net|gov|mil|biz|info|mobi|name|in|aero|jobs|museum)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM