Home  >  Article  >  Backend Development  >  Analysis of the specific application methods of PHP regular pattern modifier_PHP tutorial

Analysis of the specific application methods of PHP regular pattern modifier_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:30:12932browse

For beginnersPattern modifier:

The pattern modifier is marked outside the entire pattern.

i: The characters in the pattern will match both uppercase and lowercase letters.

m: The string is treated as multiple lines.

s: The string is treated as a single line, with newlines as ordinary characters.

x: Ignore whitespace in the pattern.

A: Force matching only from the beginning of the target string.

D: Dollar metacharacters in the pattern only match the target string The end of.

U: Matches the nearest string.

The following is a list of PHP regular pattern modifiers that may currently be used in PCRE. 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 newlines). 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" will not only match the beginning and end of the entire string, but also match after and before the newline character in it respectively. This is equivalent to Perl's /m modifier. If there are no "n" characters in the target string or ^ or $ in the pattern, setting this modifier has no effect.

s (PCRE_DOTALL)

If the PHP regular pattern 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 when escaped or in a character class. # outside the character class and all characters between 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 modifier is set, preg_replace() in the replacement string Performs normal replacement on the backreference, evaluating it as PHP code and replacing the searched string with its result.

Only preg_replace() uses this modifier, other PCRE functions ignore it. 🎜>

Note: This PHP regular pattern modifier is not available in PHP3

A (PCRE_ANCHORED)

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

D(PCRE_DOLLAR_ENDONLY)

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

S Speeds up when a pattern will be used several times. It is worth parsing it first for matching purposes. If this modifier is set, additional parsing is performed. Currently, parsing a pattern is only useful for non-anchored patterns that do not have a single fixed starting character. (PCRE_UNGREEDY)

This PHP regular pattern modifier inverts the match number value so that it is not repeated by default, but becomes repeated when followed by "?". This is incompatible with Perl. This option can also be enabled by setting the (?U) modifier in the pattern or by following the quantifier with a question mark (e.g. .*?)

X (PCRE_EXTRA)

. This modifier enables an extra feature in PCRE that is not compatible with Perl. Any backslash in the pattern followed by a letter with no special meaning causes an error, thus preserving this 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.

u (PCRE_UTF8)

Currently no other properties are controlled by this modifier. Enables an extra feature in PCRE that is not compatible with Perl. Pattern strings are treated as 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446315.htmlTechArticleFor beginners, pattern modifiers: Pattern modifiers are marked outside the entire pattern. i: Characters in the pattern Will match both uppercase and lowercase letters. m: Treat the string as multiple lines. s: Treat the string as a single...
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