PHP pcre Regular Expression Complete tutorial (1/3)

Source: Internet
Author: User
Tags character classes modifier modifiers php tutorial expression engine

PHP Tutorial pcre Regular Expression Complete tutorial
Predefined constants

Preg_pattern_order

The results are sorted by "rules", only for Preg_match_all, i.e. $matches[0] is the result of a complete rule, $matches [1] is the result of the first subgroup match
Preg_set_order:

The results are sorted by "set" only for Preg_match_all, that is, $matches[0] saves all results (including subgroups) of the first matching result, $matches [1] to save the second result information
Preg_offset_capture

In the captured result, not only the result substring, but also the position of the substring in the original string.
Preg_split_no_empty

Tell Preg_split to return only non-empty parts
Preg_split_delim_capture

Tells Preg_split to capture the contents of the bracket expression in the rule at the same time
Preg_split_offset_capture

If this tag is set, the offset position of each matching result will be returned at the same time.
Preg_no_error

If this value is returned after the Preg_last_error call, no error is shown
Preg_internal_error

If this value is returned after the Preg_last_error call, an PCRE internal error has occurred
Preg_backtrack_limit_error

If the number of backtracking exceeds the preset value, the Preg_last_error function returns this value
Preg_recursion_limit_error

If the recursive number exceeds the preset value, the Preg_last_error function returns this value
Preg_bad_utf8_error

If the final error of Preg is caused by the exception's Utf-8 data (only if run in Utf-8 mode regular expression), then Preg_last_error returns this value

Preg_bad_utf8_offset_error

If the offset does not match the starting point of the legitimate Utf-8 code (only in the case of a regular expression running in Utf-8 mode), the Preg_last_error call returns this value
Pcre_version

Returns the Pcre version number and publication date.

Pattern modifiers

I pcre_classes
Case insensitive modifiers
M pcre_multiline
Multiple-line matching, by default, pcre that the target string is composed of a set of single line characters (however it might actually contain multiple lines). The meta character ^ matches only the start position of the string, and the meta character $ matches only the end of the string, or before the new row (unless the D modifier is set). This modifier and Perl work all the time, and after use, ^ and $ will match the start and end of each row
Setting this modifier has no effect if there are no n characters in the destination string or if there are no ^ and $ $ in the pattern.
S Pcre_dotall
Used to enhance character classes. The function of (dot), by default, the dot number matches any character, but does not include line breaks and later content. With this modifier, you can match any character including line breaks
X pcre_extended
If this modifier is set, whitespace characters in the pattern are ignored unless they are escaped or in a character class. The content that follows an escaped # until the next newline character is ignored by the regular expression engine to make it easier to add comments.
E Preg_replace_eval
With this modifier, the preg_replace $replacement parameter can be used in addition to a normal reverse reference to obtain a matching value, in which the string allowed by the Eval syntax can be evaluated, and the return result is used for the final substitution.
A pcre_anchored
If this modifier is set, the pattern is forced to be "anchored" (an anchor point), which means that the constraint always starts searching from the front of the string's mouth. This effect can also be implemented by constructing the appropriate rules (this can only be done in Perl)
D pcre_dollar_endonly
If this modifier is set, $ will match the end of the target string, and if this modifier is not set, if the last character of the target string characters the newline character, $ will match the line break before the end of the string.
If the M modifier is set, the modifier is ignored. This is inconsistent with Perl.
S
When a pattern needs to be used more than once, we need to spend some time analyzing it in order to achieve higher matching efficiency. If this modifier is set, this additional analysis will be performed. Currently, this analysis of a pattern applies only to non anchor matching (i.e. no single fixed start character)
U pcre_ungreedy
This modifier reverses the greedy pattern, and if there is no such setting, by default the regular match is greedy, and the quantifier is added after the classifier is greedy, but if this modifier is set, the exact opposite is true.
This rule is incompatible with Perl.
You can also use the? U in the pattern to achieve the same effect.
X Pcre_extra
This modifier enables an additional feature that is incompatible with Perl in a pcre. Any backslash followed by a character with no special meaning can cause an error to keep these combinations for later expansion.
By default, as with Perl, a backslash followed by a character with no special meaning will be interpreted as the character intended.
No other attributes are currently controlled by this modifier
J Pcre_info_jchanged
Same as internal option? j, used to change the local pcre_dupnames option. Allow subgroups to have duplicate names
U Pcre8
This modifier opens an additional feature in the pcre that is incompatible with Perl. The pattern string is considered utf-8.

Home 1 2 3 last

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.