Regular Expression and PCRE function in PHP, php Regular Expression pcre

Source: Internet
Author: User
Tags character classes php regular expression

Regular Expression and PCRE function in PHP, php Regular Expression pcre
PCRE

PHP uses two different Regular Expressions: PCRE (Perl-compatible notation, preg _ *) function and POSIX (POSIX extension notation, ereg _ *) function. Fortunately, POSIX family functions have been discarded since PHP 5.3.0.

Regular Expression identifier

Frequently Used delimiters are forward slashes (/), hash symbols (#), and reverse symbols (~). The following examples use valid delimiters.

/foo bar/#^[^0-9]$#+php+%[a-zA-Z0-9_-]%{this is a pattern}

You can add a pattern modifier to the end separator.

Metacharacters

Some characters are given special meanings, so that they do not simply represent themselves. This encoding character with special meanings in the pattern is calledMetacharacters.

Metacharacters Description
\ It is generally used to escape characters.
^ The start position of the assertion target (or the first row in multi-row Mode)
$ The end position of the assertion target (or the end of the row in multi-row Mode)
. Match any character except linefeed (default)
[ Start character class definition
] End character class definition
| Start an optional Branch
( Start tag of the Sub-group
) End tag of the Sub-group
? As a quantizer, it indicates 0 or 1 match. It is behind the quantifiers to change the greedy nature of quantifiers. (Query quantifiers)
* Quantifiers, 0 or multiple matches
+ Quantifiers, matching once or multiple times
{ Start marking of custom quantifiers
} Custom quantifiers end mark


The section in the square brackets of the pattern is called "character class ". Only the following metacharacters are available in a character class.

Metacharacters Description
\ Escape characters
^ If it is used only as the first character (inside square brackets), it indicates that the character class is reversed.
- Mark character range
Character class

The content in square brackets is the character class.

There are some pre-defined character classes

Character class Description
\ D Any decimal number
\ D Any non-decimal number
\ H Any horizontal white space character (since PHP 5.2.4)
\ H Any non-horizontal white space character (since PHP 5.2.4)
\ S Any blank character
\ S Any non-blank characters
\ V Any vertical blank character (since PHP 5.2.4)
\ V Any non-vertical white space character (since PHP 5.2.4)
\ W Any word character
\ W Any non-word character
Atom Visible Atom

For exampleabc

Invisible Atom

For example\n

Quantifiers
Quantifiers
* Equivalent to {0 ,}
+ Equivalent to {1 ,}
? Equivalent to {0, 1}
Assertions

Simple asserted codes include \ B, \ B, \ A, \ Z, \ z, ^, and $

Forward-looking assertions

Test from current position forward

(?=) (?!)

\w+(?=;)Match a word followed by a semicolon, but the matching result does not contain a semicolon

Houzhan assertions

Test backward from current position

(?<=) (?<!)

(?<!foo)barUsed to find any bar that is not "foo"

Pattern Modifier
Pattern Modifier
U This modifier reverses the "greedy" pattern of quantifiers, making them non-Greedy by default.
I Case Insensitive match
X Ignore Blank
S The dot metacharacters match all characters, including line breaks. If this modifier does not exist, the dot does not match the linefeed.
...
PCRE Function
Preg_filter-execute a regular expression to search for and replace preg_grep-return the array entry preg_last_error in matching mode-return the error code preg_match_all generated by the last PCRE Regular Expression execution-execute a global regular expression to match preg_match-Execute a regular expression matches preg_quote-escape regular expression character preg_replace_callback_array-Perform a regular expression search and replace using callbackspreg_replace_callback-executes a regular expression search and replaces preg_replace with a callback to execute a regular expression search and replace preg_split-use a regular expression to separate strings

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.