What is the PHP regular expression pattern modifier?

Source: Internet
Author: User
Tags character classes php regular expression
The PHP pattern modifier, also known as the pattern modifiers, is used outside the delimiter of the regular expression. Mainly used to adjust the interpretation of regular expressions, to extend the regular expression in the matching, substitution and other operations of some functions, enhance the ability of the regular. But there are a lot of local explanations are wrong, it is easy to mislead others, so today I put this document out for your reference.

Pattern Correction Symbols function Description
I Match in and regular is case insensitive
M Treats a string as multiple lines. The default regular start "^" and End "$" Use the target note string as a single "line" character (even including newline characters). If you add "M" to the modifier, the start and end of each line that will be pointing to the string begins with "^" Ending and "$".
S If this modifier is set, the matched string will be treated as a line, including a newline character, and the newline character will be treated as a normal string.
X Whitespace is ignored, unless escaping is not ignored.
E used only in the Preg_replace () function, Replace the reverse reference in the replacement string with a normal substitution, evaluate it as a PHP code, and replace the searched string with its result.
A If you use this modifier, the expression must be the beginning of the matching string. For example, "/a/a" matches "ABCD".
D The $ character Fu Quan in the pattern matches the end of the target character. Without this option, if the last character is a line break, the dollar sign will also match this character. If modifier m is set, this is ignored.
E In contrast to "M", if this modifier is used, "$" will match the end of the absolute string instead of the newline character, which is turned on by default.
U Greedy mode, similar to the role of question marks, the maximum match is greedy mode.


Other information:

Pattern modifier: Explains the modifier used in the regular expression pattern

Description: The following list of modifiers that are currently possible in PCRE. The internal PCRE names of these modifiers are in parentheses. Spaces and line breaks in the modifier are ignored, and other characters cause errors.

I (pcre_caseless)

If this modifier is set, the characters in the pattern will match the uppercase and lowercase letters.

M (pcre_multiline)

By default, PCRE the target string as a single "line" character (even if it contains a line break). The line start metacharacters (^) match only the beginning of the string, and the "Line End" metacharacters ($) only match the end of the string, or the last character is preceded by a newline (unless the D modifier is set). This is the same as Perl. When this modifier is set, line start and line end match the beginning and end of the entire string, respectively, after and before the newline character. This is equivalent to Perl's/M modifier. 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 character (.) in the pattern matches all characters, including the line break. Without this setting, the line break is not included. This is equivalent to Perl's/s modifier. Exclude character classes such as [^a] always match line breaks, regardless of whether this modifier is set.

X (pcre_extended)

If this modifier is set, the whitespace characters in the pattern are ignored, in addition to being escaped or completely ignored outside of the character class, and all characters, including both ends, of the # and the next newline character outside the non-escaped characters class. This is equivalent to Perl's/x modifier, which allows annotations to be added to complex schemas. 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 that introduces conditional sub-patterns (? (middle).

E

If this modifier is set, Preg_replace () makes a normal substitution of the inverse reference in the replacement string, evaluates it as a PHP code, and replaces the searched string with its result. Only Preg_replace () uses this modifier, and the other PCRE function ignores it. Note: This modifier is not available in PHP3.

A (pcre_anchored)

If this modifier is set, the pattern is coerced to "anchored", which forces the match to start only from the beginning of the target string. This effect can also be implemented by 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 line break, the dollar sign will also match this character before (but not before any other line break). This option is ignored if the M modifier is set. There is no equivalent modifier in Perl.

S

When a pattern is to be used several times, it is worth analyzing it to speed up the match. If this modifier is set, 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 matched quantity so that it is not the default repetition, and becomes followed by "?". only to become repetitive. This is not compatible with Perl. You can also set the (? U) modifier or enable this option after the quantifier with a question mark (such as. *?).

X (Pcre_extra)

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

U (PCRE_UTF8)

This modifier enables additional features in a PCRE that are incompatible with Perl. 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. starting from PHP 4.3.5 Check the UTF-8 legitimacy of the mode.

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.