Introduction to regular expression functions in PHP _ php skills

Source: Internet
Author: User
Introduction to Regular Expression functions in PHP. For more information, see Regular Expression)

Regular expression system:
1. POSIX
2. Perl

The regex used in PHP is PCRE:
NOTE: PCRE (Perl Compatible with Regular Expressions, Perl Compatible Regular Expressions)

PCRE syntax:
1. delimiters
Must appear in pairs. any character except 0-9a-zA-Z \ can be used.
2. atom
1. the visible and invisible characters to be matched in the regular expression are both atomic.
2. a regular expression contains at least one atom
3. escape with backslash (\) when semantic symbols such as "(", "[", and "^" need to be matched

Atomic character:
\ F match the newline
\ N match the linefeed
\ R match carriage return
\ T matching tabs
\ V matches Vertical tabs

3. metacharacters
\ Escape characters
^ Match the start point of a string
$ Match the end of a string
. Match any single character except "\ n"

* Match the previous subexpression 0 or multiple times
+ Match the previous subexpression once or multiple times
? Match the previous subexpression 0 times or 1 time

{N} matches n times
{N,} matches n or more times
{N, m} matches at least n times and at most m times, (n <= m)

[] Brackets represent the atomic table, and the atomic positions in the middle are equal. When matching, match any character in the table
[^] Escape, excluding the characters contained in the atomic table.

(Pattern) matches pattern and obtains this match.
\ Num indicates the reference of the num matching obtained.


(? : Pattern) matches pattern but does not obtain this match.

(? = Pattern) forward validation pre-check, non-get match, for example: windows (? = XP | 7) windows in windows XP cannot match windows in windows 98
(?! = Pattern) indicates a positive rejection. for example, windows (?! 98 | 2000), can match windows in windows XP, cannot match windows in windows 98
(? <= Pattern) the reverse direction must be pre-checked, not to obtain matching. Example :(? <= My | Postgre) the SQL statement matches the SQL statement in MySQL and does not match the SQL statement in MSSQL.
(?
\ B match word boundary
\ B matches characters except word boundaries
    
\ D matches any number. Equivalent to [0-9]
\ D matches any character other than a number. Equivalent to [^ 0-9]

\ S matches any blank character (including spaces, tabs, and page breaks ). It is equivalent to [\ f \ n \ r \ t \ v]
\ S matches any non-blank character. It is equivalent to [^ \ f \ n \ r \ t \ v]
    
\ W matches any number, letter, or underline. It is equivalent to [0-9a-zA-Z]
\ W matches any character that is not a number, letter, or underline. It is equivalent to [^ 0-9a-zA-Z]

4. pattern modifier
I is case insensitive
M in this mode, if there is a carriage return or line feed, ^ and $ match the beginning and end of each row
S enables. matching
X Ignore blank
U ungreedy, equivalent (.*?)
A and ^ have the same effect.
D. the carriage return is not ignored at the end. when the $ character is entered at the end, the carriage return is added after the matched string, and $ can still match the string. However, after D is added, the carriage return at the end is no longer matched.

NOTE: Regular expressions are matched from left to right.


Related functions:
Preg_filter-execute a regular expression to search and replace
Preg_grep-return array entries in matching mode
Preg_last_error-return the error code generated by the last PCRE regular execution
Preg_match_all-execute a global regular expression to match
Preg_match-execute a regular expression to match
Preg_quote-escape regular expression characters
Preg_replace_callback-execute a regular expression to search and use a callback to replace
Preg_replace-search and replace a regular expression
Preg_split-use a regular expression to separate strings

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.