Regular expressions and pcre functions in PHP

Source: Internet
Author: User
Tags character classes modifiers posix

PCRE

PHP has two different ways to use regular expressions: PCRE (Perl-compatible notation, preg_*) functions, and POSIX (POSIX extended notation, ereg_*) functions. Fortunately, the POSIX family function was deprecated from PHP 5.3.0.

Regular Expression delimiter

Frequently used delimiters are forward slashes (/), hash symbols (#), and inverse symbols (~). The following example is a pattern that uses a valid delimiter

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

You can add a pattern modifier after the end delimiter

Metacharacters

Some characters are given special meanings, so that they are no longer purely representative of themselves, the pattern of this special meaning of the encoded character is called 元字符 .

Meta character Description
\ Generally used to escape characters
^ Assert the starting position of the target (or the beginning of a line in multiline mode)
$ Assert the end position of the target (or end of line in multi-line mode)
. Match any character except line break (default)
[ Start character class definition
] End character class definition
| Start an optional branch
( The start tag of the child group
) The end tag of the child group
? As a quantifier, represents 0 or 1 matches. The greedy character that is used to change quantifiers after quantifiers. (check quantifier)
* quantifier, 0 or more matches
+ quantifier, 1 or more matches
{ Custom quantifier start tag
} Custom quantifier end Tag


The part of the pattern in brackets is called the "character class". There are only the following available metacharacters in a character class

Meta character Description
\ Escape character
^ Indicates that the character class is reversed only when it is the first character (in square brackets)
- 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 arbitrary decimal digits
\d Any non-decimal number
\h Any horizontal whitespace character (since PHP 5.2.4)
\h Any non-horizontal whitespace character (since PHP 5.2.4)
\s Any whitespace character
\s Any non-whitespace character
\v Any vertical whitespace character (since PHP 5.2.4)
\v Any non-vertical whitespace character (since PHP 5.2.4)
\w Any word character
\w Any non-word character
Atomic Visible Atoms

Such asabc

non-visible atoms

Such as\n

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

The simple assertion code has \b, \b, \a, \z, \z, ^, $

forward-looking assertions

Test forward from current position

(?=)(?!)

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

post-Juniper assertions

Test backwards from the current location

(?<=)(?<!)

(?<!foo)barUsed to find any "bar" that was not preceded by "foo"

Pattern modifiers
Pattern modifiers
U This modifier reverses the "greedy" mode of quantifiers, making quantifiers default to non-greedy
I Case insensitive match
X Ignore whitespace
S The dot character matches all characters, including line breaks. If there is no modifier, the dot number does not match the line break
...
PCRE function
preg_filter — 执行一个正则表达式搜索和替换preg_grep — 返回匹配模式的数组条目preg_last_error — 返回最后一个PCRE正则执行产生的错误代码preg_match_all — 执行一个全局正则表达式匹配preg_match — 执行一个正则表达式匹配preg_quote — 转义正则表达式字符preg_replace_callback_array — Perform a regular expression search and replace using callbackspreg_replace_callback — 执行一个正则表达式搜索并且使用一个回调进行替换preg_replace — 执行一个正则表达式的搜索和替换preg_split — 通过一个正则表达式分隔字符串

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Regular expressions and pcre functions in PHP

Related Article

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.