Home  >  Article  >  Backend Development  >  What are PHP regular expression pattern modifiers?

What are PHP regular expression pattern modifiers?

零下一度
零下一度Original
2017-07-25 11:45:141321browse

PHP pattern modifiers, also called pattern modifiers, are used outside of the delimiters of regular expressions. It is mainly used to adjust the interpretation of regular expressions, expand some functions of regular expressions in matching, replacement and other operations, and enhance the capabilities of regular expressions. However, the explanations in many places are wrong and can easily mislead others, so today I compiled this document for your reference.

Mode correction symbol Function description
i Matching with regular expressions is case-insensitive
m Treat strings as multiple lines. The default regular starting "^" and ending "$" treats the target string as a single "line" of characters (even if it includes a newline character). If "m" is added to the modifier, then the start and end of each line of the string will point to the beginning of "^" and the end of "$".
s If this modifier is set, the matched string will be viewed as one line, including newlines. , newline characters will be treated as normal strings.
x Ignore whitespace unless escaped.
e is only used in the preg_replace() function, in replacement string The backreference does the normal replacement, evaluating its (i.e. "replacement string") as PHP code and using its result to replace the searched string.
A If this modifier is used, the expression must be at the beginning of the matched string. For example, "/a/A" matches "abcd".
D The $ character in the pattern matches the end of the target character. Without this option, the dollar sign will also match before the last character if it is a newline character. This is ignored if the modifier m is set.
E is the opposite of "m". If this modifier is used, then "$" will match the absolute end of the string, not before the newline character. The default is This mode is turned on.
U Greedy mode has the same effect as question mark. The maximum matching is greedy mode.


Other information:

Pattern modifiers: Explanation of modifiers used in regular expression patterns

Explanation: The following lists the current modifiers in PCRE Modifiers that may be used in . In parentheses are the internal PCRE names of these modifiers. Spaces and newlines in modifiers are ignored, other characters will cause errors.

i (PCRE_CASELESS)

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

m(PCRE_MULTILINE)

By default, PCRE treats the target string as a single "line" of characters (even if it contains newline characters) in this way). The "start of line" metacharacter (^) only matches the beginning of the string, and the "end of line" metacharacter ($) only matches the end of the string, or the last character before it if it is a newline (unless D is set) modifier). This is the same as Perl. When this modifier is set, "line start" and "line end" match in addition to the beginning and end of the entire string, they also match after and before the newline character in it. This is equivalent to Perl's /m modifier. If there is no "\n" character in the target string or no ^ or $ in the pattern, setting this modifier has no effect.

s (PCRE_DOTALL)

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

x(PCRE_EXTENDED)

If this modifier is set, whitespace characters in the pattern are completely ignored except those that are escaped or within a character class. , all characters between # outside the unescaped character class and the next newline character, inclusive, are also ignored. This is equivalent to Perl's /x modifier, allowing comments to be added to complex patterns. Note, however, that this only applies to data characters. Whitespace characters may never appear in special character sequences in a pattern, such as sequences that introduce conditional subpatterns (?( in the middle.

e

If this is set Modifier, preg_replace() performs normal replacement of the backreference in the replacement string, evaluates it as PHP code, and replaces the searched string with its result. Only preg_replace() uses this modifier, other PCRE functions. Will be ignored. Note: This modifier is not available in PHP3

A(PCRE_ANCHORED)

If this modifier is set, the mode is forced to " anchored", i.e. forcing the match to start only from the beginning of the target string. This effect can also be achieved by the appropriate pattern itself (the only way it is implemented in Perl).

D(PCRE_DOLLAR_ENDONLY)

If this modifier is set, the dollar metacharacter in the pattern will only match the end of the target string. Without this option, the dollar sign will also match if the last character is a newline character. character (but not any other newline character). This option is ignored if the m modifier is set. There is no equivalent modifier in Perl.

##When a pattern will be used several times, it is worth analyzing it first in order to speed up matching. If this modifier is set, an additional analysis will be performed. Currently, a pattern is only analyzed if there is no single fix. Useful for non-anchored patterns of start characters.

U(PCRE_UNGREEDY)

This modifier inverts the value of the match count so that it is not repeated by default. It becomes repeated by following "?". This is not compatible with Perl. It can also be done by setting the (?U) modifier in the pattern or by following the quantifier with a question mark (such as .*?). Enable this option.

X (PCRE_EXTRA)

This modifier enables an extra feature in PCRE that is not compatible with any backslash in the pattern. Following a letter with no special meaning causes an error, thus preserving the combination for future extensions. By default, as in Perl, a backslash followed by a letter with no special meaning is treated as the letter itself. Features controlled by this modifier.

u(PCRE_UTF8)

This modifier enables an additional feature in PCRE that is not compatible with Perl. UTF-8. This modifier is available since PHP 4.1.0 under Unix and since PHP 4.2.3 under win32. Patterns are checked for UTF-8 validity since PHP 4.3.5.

The above is the detailed content of What are PHP regular expression pattern modifiers?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn