Introduction to Regular expression functions in PHP _php tutorial

Source: Internet
Author: User
Tags posix
Regular Expressions (Regular expression)

Regular expression system:
1.POSIX
2.Perl

The regex used in PHP is pcre:
Note:pcre (Perl-compatible regular expression, Perl Compatible Regular Expressions)

Pcre Syntax:
1. Delimiter
must appear in pairs and can use any character except 0-9a-za-z\
2. Atoms
1. Regular need to match visible and invisible characters are atomic
2. A regular expression contains at least one atom
3. "\" Backslashes are required when matching symbols such as "(", "[" "^", "^", and so on, are escaped

Atomic characters:
\f Match page break
\ nthe match line break
\ r Match Carriage return character
\ t matches tabs
\v Matching Vertical tabs

3. Meta-Characters
\ escape Character
^ Match string starting point
$ match End of string
. Match any single character except "\ n"

* Match previous sub-expression 0 or more
+ Match Previous sub-expression 1 or more times
? Match previous subexpression 0 or 1 times

{n} matches n times
{n,} matches n or n times
{n,m} minimum match n times up to M times, (n<=m)

[] The brackets represent the atomic table, and the atomic status in the middle is equal. Matches any one of the characters in the table when matching
[^] Caron, excluding the characters contained in the following atomic table.

Pattern matches the pattern and gets the match.
\num a reference to the fetch of the first NUM match.


(?:p Attern) matches pattern but does not get this match

(? =pattern) positive pre-check, non-get match, for example: Windows (? =xp|7) can match windows in WindowsXP Windows98 does not match
(?! =pattern) positive negation for non-fetch matching, for example: Windows (?! 98|2000), can match windows in WindowsXP, does not match windows in Windows98
(? <=pattern) Reverse positive pre-check, non-acquisition matching. For example: (? <=my| postgre) SQL can match SQL in MySQL and cannot match SQL in MSSQL
(?
\b Match word boundaries
\b Matches characters other than the word boundary
    
\d matches any one number. equivalent to [0-9]
\d matches any character other than a non-digit. equivalent to [^0-9]

\s matches any white space character (including spaces, tabs, page breaks, and so on). equivalent to [\f\n\r\t\v]
\s matches any one non-whitespace character. equivalent to [^\f\n\r\t\v]
    
\w matches any number, letter, or underscore. equivalent to [0-9a-za-z]
\w matches any character that is not a number, letter, or underscore. equivalent to [^0-9a-za-z]

4. Mode modifier
I is case insensitive
M if there is a carriage return or line break in this mode, ^ and $ will match the beginning and end of each line
s let. can match \ n
x Ignore whitespace
U cancel Greed, equal to (. *?)
A is the same as the ^ effect
D at the end does not ignore the carriage return, at the end there is a $ character, after the matching string followed by a carriage return, $ will still be able to match it success. But with D, the end of the carriage return, no longer matches

Note: Regular expressions are matched from left to right.


Related functions:
preg_filter-perform a regular expression search and replace
preg_grep-returns an array entry for the matching pattern
preg_last_error-returns the error code generated by the last Pcre regular execution
Preg_match_all-performs a global regular expression match
Preg_match-performs a regular expression match
Preg_quote-escaping regular expression characters
Preg_replace_callback-performs a regular expression search and replaces it with a callback
preg_replace-perform a search and replace of a regular expression
Preg_split-separating strings by a regular expression

http://www.bkjia.com/PHPjc/325110.html www.bkjia.com true http://www.bkjia.com/PHPjc/325110.html techarticle Regular expression (Regular expression) Regular expression system: 1.POSIX 2.Perl the regex used in PHP is PCRE:NOTE:PCRE (Perl-compatible regular expression, Perl Compatible Regular Expressions) PCR ...

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