Important php regular expressions, php regular expressions _ PHP Tutorial

Source: Internet
Author: User
Tags php regular expression
Php regular expressions are very important. A very important php regular expression explanation. a php regular expression is a very important knowledge point in php. it is usually used to find and replace strings, the most commonly used is the explanation of php regular expressions that are important for verification, and the explanation of php regular expressions

Regular expressions are a very important knowledge point in php. they are usually used to search for and replace strings. The most common thing is to verify whether the format of user input information is correct, such as the Mail format and telephone format. In software such as collectors, regular expressions are also mandatory!
Now let's start to learn the basic syntax of regular expressions:
1. "/" is the delimiter, and "/" is the pattern to be matched in the target object.In addition, to make the regular expression more flexible, metacharacters ("+", "*", and "?" are introduced.
(1) the "+" metacharacter specifies that the leading character must appear one or more times consecutively in the target object.
For example,/php +/can match with "phpp", that is, one or more p strings appear after the letter ph.
(2) the "*" metacharacter specifies that the leading character must appear zero or multiple times in the target object
For example,/php */can match with "phpddt", that is, there can be 0 or more p after ph
(3) "?" Metacharacter specifies that the leading object must appear zero or once consecutively in the target object.
For example:/php? /Match with "pher", that is, there can be 0 or 1 p after phh
Other important metacharacters:

  • \ S: used to match a single space character, including the tab key and line break;
  • \ S: used to match all characters except a single space character;
  • \ D: used to match numbers from 0 to 9;
  • \ W: used to match letters, numbers, or underscores;
  • \ W: used to match all characters that do not match \ w;
  • .: Used to match all characters except line breaks.

Example: // \ s +/is used to match one or more space characters in the target object
2. the locator is used to specify the location where the matching mode appears in the target object. Commonly used include "^", "$", "\ B", and "\ B"
(1) the "^" operator specifies that the matching mode must start with the target string.
(2) the "$" operator specifies that the matching mode must appear at the end of the target object.
(3) the \ B locator specifies that the matching mode must appear at the beginning or end of the target string.
(4) the "\ B" locator specifies that the matched object must be within the boundary of the start and end of the target string.
3. php's regular expression matching mode is very flexible. you can specify a certain range.
For example:
/[A-Z]/
The above regular expression will match any uppercase letter from A to Z.
/[A-z]/
The above regular expression will match any lowercase letter from a to z.
/[0-9]/
The above regular expression will match any number from 0 to 9.
/([A-z] [A-Z] [0-9]) +/
The above regular expression will be associated with any string consisting of letters and numbers
4. it can be matched with multiple modes at the same time
For example,/phpddt.com | phpddt | 100/can match "phpddt.com" "phpddt" "100"
5. the negation "[^]" specifies that the target object cannot contain strings specified in the mode.
For example, [^ phpddt] matches everything except the phpddt character.
Let's talk about common regular expression functions! (Very important)

<? Php // preg_match ("regular expression", "string") is used to find the match in the string $ email = "987044391@qq.com"; if (preg_match ("/^ ([a-zA-Z0-9]) + ([. a-zA-Z0-9 _-]) * @ ([. a-zA-Z0-9 _-]) + ([. a-zA-Z0-9 _-] +) + ([. a-zA-Z0-9 _-]) $/", $ email) {echo 'matched
 ';} Else {echo' match failed
 ';} // Preg_quote ("string") add an escape character \ $ str = "php point-to-point is a website for learning php before each regular expression syntax, (⊙ o ⊙ )... "; Echo preg_quote ($ str); echo"
 "; // Preg_split (" regular "," string ") split string $ php =" + php ++ dot link ++ is a good website "; $ field = preg_split ("/\ + {1,}/", $ php); foreach ($ field as $ f) {echo $ f. "";} echo"
 "; // Preg_grep (" regular "," string ") matches the array and returns the new array $ phpddt = array (" php "," php100 "," haha ", "hahaha", "phpchina"); $ item = preg_grep ("/^ php/", $ phpddt); print_r ($ item); echo"
 "; // Preg_replace (" regular "," Replace content "," original string ") very important, very often $ a =" Welcome to http://www.bkjia.com /"; // add the hyperlink echo preg_replace ("/http :\/\/(. *) \ // "," \$ {0} "," $ a ");?>

The above is all of the content in this article, hoping to help you learn php regular expressions.

Articles you may be interested in:
  • PHP extracts the image address from the string [regular expression]
  • PHP regular expression recommendation
  • Implementation and explanation of regular expressions for email addresses in php
  • A simple method for matching URLs using regular expressions in php
  • Php regular expression matching any attribute in img
  • Php uses regular expressions to extract strings in the brackets, parentheses, brackets, and braces.
  • Example of crawling webpage data using curl and regular expression in php
  • Php filters HTML tags, attributes, and other regular expressions
  • Php strings and regular expressions

Regular expression is a very important knowledge point in php. it is usually used to search for and replace strings. The most commonly used is to test...

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.