Regular expression to match email address in php_PHP tutorial

WBOY
Release: 2016-07-13 10:44:43
Original
1484 people have browsed it

An example of a regular expression for matching email addresses in PHP. The regular matching expression I commonly use when replacing email addresses: /^[a-z]([a-z0-9]*[-_]?[a-z0 -9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z ]{2})?$/i, the following is a detailed analysis for friends in need.

php example

The following uses PHP as an example:

 代码如下 复制代码
< ?php
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!”;
}
?>

Description:
①/content/i forms a case-insensitive regular expression; ^ starts the match; $ ends the match.

②[a-z] E-Mail prefix must start with an English letter

③([a-z0-9]*[-_]?[a-z0-9]+)* matches _a_2, aaa11, _1_a_2, but does not match a1_, aaff_33a_, a__aa, if it is a null character , is also matched, * means 0 or more.

④* represents 0 or more preceding characters.

⑤[a-z0-9]* matches 0 or more English letters or numbers; [-_]? matches 0 or 1 "-", because "-" cannot appear continuously.

⑥[a-z0-9]+ matches 1 or more English letters or numbers, because "-" cannot be used as the end

⑦@ There must be someone @

⑧([a-z0-9]*[-_]?[a-z0-9]+)+ See above ([a-z0-9]*[-_]?[a-z0-9 ]+)* explanation, but it cannot be empty, + means one or more.

⑨[.] treats special characters (.) as ordinary characters; [a-z]{2,3} matches 2 to 3 English letters, usually com or net, etc.
⑩([.][a-z]{2})? Matches 0 or 1 [.][a-z]{2} (such as .cn, etc.) I don’t know if the last part of .com.cn is usually two bit, if not, please change {2} to {number of starting words, number of ending words}


js example

The code is as follows
 代码如下 复制代码

<script><br> function Email(ee){<br> var emailreg = "^w+@w+.w+(.w+)*$";</p> <p>var rege = new RegExp(emailreg, 'g');<br> alert(rege.test(ee));<br> }<br> var ee1 = "12xwz@123e^rsrf6.csdfdfom.df";<br> var ee2 = "12xwz@123ersrf6.csdfdfom.df";<br> Email(ee1);<br> Email(ee2);<br> </script>

Copy code



<script><br> function Email(ee){<br> var emailreg = "^w+@w+.w+(.w+)*$";<br> <br>var rege = new RegExp(emailreg, 'g');<br> alert(rege.test(ee)); }</td> var ee1 = "12xwz@123e^rsrf6.csdfdfom.df";</tr> var ee2 = "12xwz@123ersrf6.csdfdfom.df";</table> Email(ee1); Email(ee2);<p align="left"> </script>

In order for you to better master regular expressions, please learn the string escape form Here, you use a string to represent the regular expression, and the escape character must be expressed. If you want to match, use ^w+@w+.w+(.w+)*$ It starts with w word and has one or more @后 It’s a word .followed by a word with one or more (.w+)* can have none or multiple
http://www.bkjia.com/PHPjc/633085.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633085.htmlTechArticleAn example of a regular expression for matching email addresses in PHP. Regular expressions for replacing regular email addresses. My commonly used regular matching expressions: /^[a-z]([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9 ]+)+[\.][a-z...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template