PHP regular expression summary

Source: Internet
Author: User
PREG_SPLIT_OFFSET_CAPTURE // The pattern offset value is recorded in the PHP regular expression of the array.

[Posix regular expression meanings and symbols]
1. portable Operation System interface
2. Composition of atoms, pattern modifiers, and child tokens
3. | pipeline operators can be understood as OR, meaning that multiple regular expressions coexist. php | zend
4. () indicates matching a group of p (hp) * => php phphp p
5, [] indicates a certain range of [^] Only in this case is not [^ a-zA-Z0-9 _]
6. (); represents a group of atoms and references.
7 ,? Match 0 times or once or non-greedy match
8. ^ at the beginning, it is called "start" and "reverse" is represented in the matching mode.
[Meaning of quantifiers]

Symbol Description
+ > = 1 repeated times at least once
* > = 0 repeated times at least zero
? 0 or 1 (non-greedy in a particular scenario)
{N ,} > = N appears at least N times
{N, m} N <= Times <= m between n and m
{N} Match n times
. Match any character
^ Match the start or reverse
$ End

[Built-in matching mode]

Symbol Description
[: Alnum:] Matching number, letter [a-zA-Z0-9]
[: Alpha:] Match uppercase and lowercase letters [A-Za-z]
[: Cntrl:] Control characters, such as tabulation backslashes
[: Digit:] Matching number [0-9]
[: Graph:] Matching printable characters, ASCII33-126
[: Lower:] Match lowercase letters [a-z]
[: Upper:] Match uppercase letters [A-Z]
[: Punct:] Matching punctuation division \ |
[: Space:] \ N \ t \ r and other blank characters
[: Xdigit:] Hexadecimal [a-f0-9A-F]

Posix functions]
1. ereg ($ pattern, $ string, & array); eregi () ignores the case sensitivity of $ string // matches and returns
2. ereg_replace ($ pattern, $ replacement, $ string); // Match and replace
3. split ($ pattern, $ string, $ limit) // Match and separate

[Perl regular pcre]
1. modifier
I: // case insensitive
G: // search for preg_match_all in the full sentence (error reported. The reason is unknown)
M: // replace the character with multiple rows. each row ends with a ^ start $.
S: // see the string as a line
X: // Ignore the blank and comments in the regular expression
U: // after the first match, can it be stopped? The question mark in posix cannot be used as a non-greedy match.
For example,/apple/I matches the word "apple", which is case-insensitive.
/Banan/ig: // Match banana, ignore the case sensitivity of the string, and search globally
Preg_match ()
Add '/pattern/''to the pcre pattern | ';
Appendix:
I: if "I" is added to the modifier, the regular expression will cancel the case sensitivity, that is, "a" and "A" are the same.
M: The default regular start "^" and end "$" only means that if "m" is added to the modifier of the regular string, the start and end will refer to each row of the string: each line starts with "^" and ends with "$ ".
S: if "s" is added to the modifier, the default "." indicates that any character except the line break will become any character, that is, include a line break!
X: if this modifier is added, spaces in the expression will be ignored unless it has been escaped.
E: This modifier is only useful for replacement, which indicates to use as PHP code in replacement.
A: If this modifier is used, the expression must be the start part of the matched string. For example, "/a/A" matches "abcd ".
E: opposite to "m". if this modifier is used, "$" matches the end of the absolute string instead of the line break. this mode is enabled by default.
U: Similar to question mark, used to set "greedy mode ".

[Pcre built-in mode]

Symbol Description
\ B Match the boundary/abc \ B/; string ending with abc
\ B Match
\ D Matching number
\ D Match non-numbers
\ S Match blank characters
\ S Match blank characters
\ W Match letters and numbers underline ^ [a-z0-9A-Z _] $
\ W Match letters and underscores

[Pcre regular function]
1. preg_match ($ pattern, $ string, & $ result );
2. preg_grep ($ pattern, $ array, $ flag); $ flag = true
3. preg_match_all ($ pattern, $ string, & $ result, order)
Order = PREG_PATTERN_ORDER (default) | PREG_SET_ORDER;
4. preg_quote ($ string); escape characters with special regular meanings in string
5. preg_replace ($ pattern, 'reverse reference | $ replace ', $ string, $ limit );
Anti-reference: \ 0 \ 1 \ 2 ...... \ 99
6. preg_split ($ pattern, $ string, $ limit, $ flag)
$ Flag:
PREG_SPLIT_NO_EMPTY // remove null values from the array
PREG_SPLIT_DELIM_CAPTURE // the sub-mode value is also saved to the array
PREG_SPLIT_OFFSET_CAPTURE // The value of the pattern offset is recorded in the array.
7. preg_replace_callback ($ pattern, callback, $ string, $ limit)
[Note]
1. posix regular expressions do not require/pattern/, while pcre regular expressions do. |/
2 ,? Only after quantifiers in pcre can represent non-greedy
3. U and? Invalid at the same time
4. pay attention to sub-pattern matching starting from \ 0 for anti-reference.
5. all the characters in [] are processed as common characters and do not need to be escaped to represent the range.
6. ^ symbols only represent non-meanings in [], and [] indicates the start of a symbol.
7. $ and ^ can only start and end with the matching mode.
8. do not confuse the built-in modes in posix and pcre.
9. the built-in mode in posix is a range. when using it, add [] for example [[: alpha:].


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.