Analysis of the problem of the PHP regular expression matching mailbox

Source: Internet
Author: User
Tags php regular expression
This article describes an issue that is encountered when using regular expressions in PHP to match a mailbox (email), and a workaround. Please refer to the friends who are in need. The

PHP regular expression matches the mailbox with the following code:

 

Why is the output above legitimate? {2,10} Here is not only 2 to 10 digits, but the above mailbox is more than 10 bits. This regular match is matched to the 1412424545645454 mismatch in the front part of this section ' 545454545k@qq.com ', so it's still legal.

Matches correctly and can be modified to:

$a = '/^ ([\w\.\_]{2,10}) @ (\w{1,}). ([a-z]{2,4}) $/';

Note the regular:/([\w\.\_]{2,10}) @ (\w{1,}). ([a-z]{2,4})/It has no assertion, meaning that as long as there is a match in the string, the {2,10} is only 2 to 10 bit matched, but not for the entire string as long as there is a partial match. An assertion is used to determine whether a whole string is matched. such as ^ $/^ ([\w\.\_]{2,10}) @ (\w{1,}). ([a-z]{2,4}) $/Note that the header and tail add ^ $

Perfect code, as follows:

 
  • 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.