Regular expression
The code is as follows: |
Copy code |
^ [_. 0-9a-z-] + @ ([0-9a-z] [0-9a-z-] +.) + [a-z] {2, 3} $
|
In this regular expression, "+" indicates that one or more strings appear consecutively. "^" indicates that the next string must start, "$" indicates that the previous string must appear at the end;
"." Is also ".". Here "" is an escape character; "{2, 3}" indicates that the previous string can appear 2-3 times in a row. "()" Indicates that the contained content must appear in the target object at the same time. "[_. "0-9a-z-]" indicates that it is included in "_", ". ","-", letters from a to z, any characters in numbers from 0 to 9;
In this way, the regular expression can be translated as follows:
The following characters must start with (^), "_", and ". ","-", letters from a to z, numbers from 0 to 9 ([_. 0-9a-z-] "," the preceding character must appear at least once (+) ", @," the string starts with a character that contains a letter in the range from a to z and a number in the range from 0 to 9, followed by at least one character in "-", any letter from a to z, any number from 0 to 9, and finally in. end ([0-9a-z] [0-9a-z-] + .)) "," the preceding character appears at least once (+) ", and" Letters From a to z appear 2-3 times, end with it ([a-z] {2, 3} $ )"
The code is as follows: |
Copy code |
Function is_valid_email ($ email, $ test_mx = false) { If (eregi ("^ ([_ a-z0-9-] + )(. [_ a-z0-9-] +) * @ ([a-z0-9-] + )(. [a-z0-9-] + )*(. [a-z] {2, 4}) [wind_phpcode_0] quot;, $ email )) If ($ test_mx) { List ($ username, $ domain) = split ("@", $ email ); Return getmxrr ($ domain, $ mxrecords ); } Else Return true; Else Return false; } |
A domain name is composed of a specific character set, English letters, numbers, and "-" (that is, a hyphen or minus sign) of Chinese characters in different countries, but cannot start or end with "-". "-" cannot appear consecutively. The domain name is case-insensitive. The domain name can contain up to 60 bytes (including the suffix. com,. net, and. org ).
/^ [A-z] ([a-z0-9] * [-_]? [A-z0-9] +) * @ ([a-z0-9] * [-_]? [A-z0-9] +) + [.] [a-z] {2, 3} ([.] [a-z] {2 })? $/I;
/Content/I forms a case-insensitive regular expression;
^ Match starts
$ Match ended
[A-z] the e-mail prefix must start with an English letter
([A-z0-9] * [-_]? [A-z0-9] +) * and _ a_2, aaa11, _ 1_a_2 match, and a1 _, aaff_33a _, a _ aa does not match, if it is a null character, but also match, * indicates 0 or more.
* Indicates 0 or more characters.
[A-z0-9] * matches 0 or more English letters or numbers
[-_]? Match 0 or 1 "-" because "-" cannot appear continuously
[A-z0-9] + matches one or more English letters or numbers because '-' cannot end
@ There must be @
([A-z0-9] * [-_]? [A-z0-9] +) + See above ([a-z0-9] * [-_]? [A-z0-9] +) * interpreted, but cannot be empty, + represents one or more.
[.] Use special characters (.) as common characters
[A-z] {2, 3} matches two to three English letters, generally com or net.
([.] [A-z] {2 })? Match 0 or 1 [.] [a-z] {2} (for example. cn, etc.) I do not know whether the last part of .com.cn is generally two. If not, change {2} to {start word count, end word count}
Perfect E-Mail regular expression, with a detailed explanation, please help test it! 2. Extract the email from the string:
The code is as follows: |
Copy code |
<? Php Function getEmail ($ str ){ $ Pattern = "/([a-z0-9] * [-_.]? [A-z0-9] +) * @ ([a-z0-9] * [-_]? [A-z0-9] +) + [.] [a-z] {2, 3} ([.] [a-z] {2 })? /I "; Preg_match_all ($ pattern, $ str, $ emailArr ); Return $ emailArr [0]; } $ Emailstr = "9999@qq.com.cn I'm not a vi place open iid mailing list: fuyongjie@163.com and hh@qq.com; ..;, fuyongjie.100@yahoo.com, fu-1999@sina.com "; $ EmailArr = getEmail ($ emailstr ); Echo "<pre> "; Print_r ($ emailArr ); Echo "</pre> "; ?> Print as follows: Array ( 9999@qq.com.cn Fuyongjie@163.com Hh@qq.com Fuyongjie.100@yahoo.com [4] => fu-1999@sina.com ) 3. Comparison: the regular expressions in 2nd do not contain the ^ and $ Values of 1st; |
View instances again
The code is as follows: |
Copy code |
Function funcemail ($ str) // email regular expression { Return (preg_match ('/^ [_. 0-9a-z-a-z-] + @ ([0-9a-z] [0-9a-z-] + .) + [a-z] {2, 4} $/', $ str ))? True: false; } // Verification method 1 $ Str = "qbcd@126.com.cn "; Preg_match ("/^ [0-9a-z] + @ ([0-9a-z] +) [.]) + [a-z] {2, 3} $/", $ str, $ re ); Print_r ($ re); // email verification 2 If (eregi ("^ [_. 0-9a-z-] + @ ([0-9a-z] [0-9a-z-] + .) + [a-z] {2, 3} $ ", $ email )){ Echo "your email has passed the preliminary check "; } // Third mailbox verification method If (ereg ("/^ [a-z] ([a-z0-9] * [-_.]? [A-z0-9] +) * @ ([a-z0-9] * [-_]? [A-z0-9] +) + [.] [a-z] {2, 3} ([.] [a-z] {2 })? $/I; ", $ email )){ Echo "your email address is correct! ";} Else { Echo "please try again! "; } |