PHP Regular expression pattern modifier and Reverse Reference use introduction _ Regular Expression

Source: Internet
Author: User
Tags character classes lowercase modifier modifiers php code php regular expression
Matching sequence of regular expressions:
1. Mode unit
2. Duplicate match? * + {}
3. Boundary Limit ^ $ b b
4. Mode Selection |

pattern modifier:
The pattern modifier is marked outside the entire pattern.

I: Characters in the pattern will match both uppercase and lowercase letters.
M: string as multiple lines.
S: Treats a string as a single line, and a newline character as a normal.
x: Ignore whitespace in the pattern.
A: Coercion only starts at the beginning of the target string.
D: The dollar character in the pattern matches only the end of the destination string.
U: Matches the nearest string.

schema modifiers in PHP and regular expressions

The following is a list of possible modifiers that are currently available in PCRE. The internal PCRE names of these modifiers are in parentheses.

I (pcre_caseless)
If you set this modifier, the characters in the pattern will match both the uppercase and lowercase letters.

M (pcre_multiline)
By default, PCRE the target string as a single "line" character (even if it contains newline characters). The "line Start" metacharacters (^) only match the start of the string, and the line end Metacharacters ($) match only the end of the string, or the last character is preceded by a newline character (unless the D modifier is set). This is the same as Perl.

When this modifier is set, line start and end of line match the end of the entire string, and the after and before the line breaks are matched respectively. This is equivalent to the/m modifier of Perl. If there is no "\ n" character in the target string or there is no ^ or $ in the pattern, setting this modifier has no effect.

S (pcre_dotall)
If this modifier is set, the dot meta character (.) in the pattern matches all characters, including line breaks. Without this setting, line breaks are not included. This is equivalent to Perl's/s modifier. Excluding character classes such as [^a] always match newline characters, regardless of whether this modifier is set.

X (pcre_extended)
If this modifier is set, whitespace characters in the pattern are completely ignored in addition to being escaped or outside of the character class, and all characters, including both ends, that are outside of the escaped character class and the next newline character are ignored. This is equivalent to Perl's/x modifier, allowing annotations to be added to complex patterns. Note, however, that this applies only to data characters. Whitespace characters may never appear in a special sequence of characters in a pattern, such as a sequence (?) in which a conditional child pattern is introduced.

E
If this modifier is set, preg_replace () substitutes the reverse reference in the replacement string as a PHP code and replaces the searched string with the result.

Only Preg_replace () uses this modifier, and the other PCRE functions are ignored.

Note: This modifier is not available in PHP3.

A (pcre_anchored)
If this modifier is set, the pattern is coerced to "anchored", which means that only the beginning of the target string is forced to match. This effect can also be implemented through the appropriate pattern itself (the only method implemented in Perl).

D (pcre_dollar_endonly)
If this modifier is set, the dollar character in the pattern matches only the end of the target string. Without this option, if the last character is a newline, the dollar sign also matches the character before it (but does not match any other line breaks). This option is ignored if the M modifier is set. There is no equivalent modifier in Perl.

S
When a pattern is used several times, it is worthwhile to analyze it for the sake of accelerated matching. If this modifier is set, an additional analysis is performed. Currently, parsing a pattern is only useful for non-anchored patterns that do not have a single fixed starting character.

U (Pcre_ungreedy)
This modifier reverses the value of the matching number so that it is not the default repetition, and becomes followed by the "?" to become repetitive. This is not compatible with Perl. It can also be set in the pattern (? U) modifier to enable this option.

X (Pcre_extra)
This modifier enables an additional feature that is incompatible with Perl in a PCRE. Any backslash in the pattern followed by a letter with no special meaning causes an error, thus preserving this combination for future expansion. By default, as with Perl, a backslash is followed by a letter that has no special meaning. No other features are currently controlled by this modifier.

U (PCRE_UTF8)
This modifier enables an additional feature that is incompatible with Perl in a PCRE. The pattern string is treated as UTF-8. This modifier is available under Unix from PHP 4.1.0 and is available under Win32 from PHP 4.2.3.

about reverse References

The reverse reference and the child mode are related.

/()/is the delimiter for the child mode, the matching child pattern is automatically assigned number 1~9, so because the \\99 will make the interpreter feel confused. But can use/(?) /(Another way of writing/(?) Word "/") to name the child mode, using \\k (must remember a k) to reverse the reference, so there will be no number of restrictions (the exact maximum is 99, and then it seems vaguely unnecessary).

/(?: t1|t2|) /This "?:" is used to tell the interpreter not to assign AutoNumber to this child mode, so it is "jump to" T2 start numbering.

/\b (? =ing\b)/This refers to the previous content of the matching expression if the example is used to match I ' m singing while you dancing the resulting result is sing and danc because the word qualifier is \b.

/(? <=ing)/match the content after Ing.

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.