Phpemail Regular Expression explanation-PHP source code

Source: Internet
Author: User
This article only briefly introduces the e-mail Regular Expression in php. For more information, see. This article only briefly introduces the e-mail Regular Expression in php. For more information, see.

Script ec (2); script

General e-mail, such as zhangshan@163.com, abc@sina.com.cn such as some common form of the line, but in some of our company's customers mailbox but there are some zhangshna.Mr@163.com, abc_Wang.dd@sian.com, abc_Wang.dd.cc@sian.com this kind of similar form, there are points before the @ symbol ., I used it for use, but I can't do it now. I have to study Regular Expressions myself.

If you have any questions about regular expressions, please leave a message here for discussion!

Original Regular Expression

The Code is as follows:

/^ [A-zA-Z0-9 _-] + @ [a-zA-Z0-9 _-] + (. [a-zA-Z0-9 _-] +) + $ /;

The modified regular expression based on my actual situation

The Code is as follows:

/^ (W) + (. w +) * @ (w) + (. w {2, 3}) {1, 3}) $ /;

Or

/^ (W) + (. w +) * @ (w) + (. w +) $ /;


First, start with the name and divide it:
(1) email prefixes on the left: letters, numbers, underscores, points, and minus signs (can only start and end with a number or letter)
(2) Right domain name rules-letters, numbers, and minus signs (can only begin with a number and a letter, and cannot exceed 63 characters)

Now that the rules are ready, the rest is matching, hoping to avoid a "bad guy" as much as possible ":

The Code is as follows:
[A-zd] ([a-zd _. -] * [a-zd]) * @ ([a-zd] [a-zd-] {0, 61} [a-zd].) {1, 3} [a-z] {2, 6} ([.] [a-z] {2, 3 })? Right side
Add ^ $ I:/^ [a-z0-9] ([a-z0-9-_.] * [a-z0-9]) * @ ([a-z0-9] [a-z0-9-] {} [a-z0-9].) {1, 3} [a-z] {2, 6} ([.] [a-z] {2, 6 })? $/I

I tried PHP and it worked well, that is, some problems still occurred in SQL statements, but it basically met my requirements.
Character Description:
^: Match the start position of the input.
: Mark the next character as a special character or literal value.
*: Match the first character Zero or several times.
+: Match the previous character once or multiple times.
(Pattern) matches the pattern and remembers the matching.
X | y: matches x or y.
[A-z]: a character in a certain range. Matches any character in the specified range.
W: matches any word characters, including underscores.

{N, m} matches at least n times and at most m times
$: Matches the end of the input.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.