Introduction to the specific usage rules of the PHP regular expression preg_match _php tutorial

Source: Internet
Author: User
use of the PHP regular expression preg_match:

With Preg_match (), we can complete the rule matching of strings. If a match is found, the Preg_match () function returns 1, otherwise 0 is returned. There is also an optional third parameter that allows you to put a matching part in an array. This functionality can become very useful when validating data.

 
  
  
  1. $ string "Football";
  2. }

The above example will match successfully, because the word football contains foo. Now let's try a more complicated one, such as verifying an Email address.

 
  
  
  1. $ string "First.last@domain.uno.dos"

  2. ([.] [a-za-z0-9_]+) *[@][a-za-z0-9_]+ ([.]

This example verifies that the email address is in the correct format.

Rules for PHP Regular expression preg_match:

We will look at the various rules represented by this regular expression through the demonstration of the above example.

PCRE, as the name implies, has the same syntax as regular expressions in Perl, so each regular expression must have a pair of delimiters. We generally use/as delimiters.

Start with ^ and end of $ let PHP check from the beginning of the string to the end. If there is no $, the program will still match to the end of the Email.

[and] are used to restrict the type of license input. For example A-Z allows all lowercase letters, a-Z allows all uppercase letters, 0-9 all numbers, and more, among other types.

{and} are used to limit the number of characters expected. For example, {2,4} indicates that each section of a string can have a length of 2-4 characters, such as. com.cn or. Info. Here, "." does not count as a character, because the type of license input defined before {2,4} is only uppercase and lowercase, so the segment matches only the uppercase and lowercase letters

(and) are used to combine subsections and define the characters that must exist in the string. (A|b|c) to match A or B or C.

(.) will match all characters, while [.] Only matches "." itself.

To use some of the symbols themselves, one must be added first. These characters are: () []. * ? + ^ | $

PHP Regular expression Preg_match related content to introduce you here, I hope you understand and grasp the PHP preg_match regular expression helpful.


http://www.bkjia.com/PHPjc/446307.html www.bkjia.com true http://www.bkjia.com/PHPjc/446307.html techarticle PHP Regular Expression preg_match use: Using Preg_match (), we can complete the rule matching of the string. If a match is found, the Preg_match () function returns 1, otherwise 0 is returned. ...

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