PHP—PCRE regular expression pattern modifier

伊谢尔伦
Release: 2016-11-21 17:07:26
Original
1309 people have browsed it

The currently available PCRE modifiers are listed below. The names mentioned in brackets are the names of these modifiers internal to PCRE. Spaces and newlines in pattern modifiers will be ignored, and other characters will cause errors.

i (PCRE_CASELESS)

If this modifier is set, letters in the pattern will be matched case-insensitively.

m (PCRE_MULTILINE)

By default, PCRE thinks that the target string consists of a single line of characters (however in fact it may contain multiple lines), the "start of line" metacharacter (^) only matches the beginning of the string position, and the "end of line" metacharacter ($) only matches the end of the string, or the final newline character (unless the D modifier is set). This behavior is the same as perl. When this modifier is set, "beginning of line" and "end of line" will match before or after any newline character in the target string. In addition, they will also match the beginning and end of the target string respectively. This is equivalent to perl's /m modifier. If there are no "n" characters in the target string, or ^ or $ do not appear 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 modifier, the period does not match newline characters. This modifier is equivalent to the /s modifier in perl. A negated character class such as [^a] always matches a newline character, regardless of the setting of this modifier.

x (PCRE_EXTENDED)

If this modifier is set, whitespace data characters in the pattern that are not escaped or are not in a character class will always be ignored, and # characters that are outside an unescaped character class will always be ignored Characters between and the next newline are also ignored. This modifier is equivalent to the /x modifier in Perl and enables comments to be included in the compiled schema. NOTE: This is for data characters only. Blank characters still cannot appear in the special character sequence of the pattern. For example, the sequence (?( introduces a conditional subgroup (Annotation: If a blank character appears in the special character sequence defined by this syntax, a compilation error will occur. For example, (?( will will cause an error).

e (PREG_REPLACE_EVAL)

If this modifier is set, preg_replace() will evaluate and execute the replaced string as PHP code after performing the back reference replacement of the replacement string. Function mode), and use the execution result as the string actually involved in the replacement. Single quotes, double quotes, backslash () and NULL characters will be escaped with backslashes when backreference replacement.


Note: Please ensure that the replacement parameter consists of a valid PHP code string, otherwise PHP will generate an interpretation error on the line where preg_replace() is called. Only preg_replace() uses this modifier, other PCRE functions ignore this modifier.

A (PCRE_ANCHORED)

If this modifier is set, the pattern is forced to be an "anchored" pattern, which means that the match is constrained to search only from the beginning of the target string. This effect can also be constructed using the appropriate pattern. , and this is the only way to implement this pattern in Perl

D (PCRE_DOLLAR_ENDONLY)

If this modifier is set, the metacharacter dollar sign in the pattern only matches the end of the target string if this modifier is not set. , when the string ends with a newline character, the dollar sign will also match that newline character (but not any preceding newline character). If the modifier m is set, this modifier is ignored. There is no such thing in perl. Modifier equivalent to modifier.

S

When a pattern needs to be used multiple times, it is worth spending some time to do some additional analysis if this modifier is set in order to improve the matching speed. The analysis will be performed. Currently, this analysis of a pattern only applies to unanchored pattern matches (i.e. without a single fixed start character)

U (PCRE_UNGREEDY)

This modifier reverses the " of the quantifier. Greedy" mode. Makes the quantifier non-greedy by default. You can make it greedy by following the quantifier with ?. This is incompatible with perl. It can also be set using the intra-mode modifier setting (?U) , or mark it as non-greedy with a question mark (such as .*?)


Note: In non-greedy mode, characters exceeding pcre.backtrack_limit

X (PCRE_EXTRA)

are usually not matched. character opens the attachment function that is incompatible with PCRE and perl. Any backslash in the pattern followed by a character with no special meaning will result in an error, these characters are preserved for backward compatibility. By default, in perl, a backslash followed by a character with no special meaning is considered the original text of that character. No other features are currently controlled by this modifier.

J (PCRE_INFO_JCHANGED)

Internal option settings (?J) modify the local PCRE_DUPNAMES options. Allow subgroups with duplicate names, (Annotation: can only be set through internal options, external /J settings will cause errors.)

u (PCRE_UTF8)

This modifier turns on an additional feature that is incompatible with perl. Pattern strings are considered UTF-8. This modifier is available starting with PHP 4.1.0 or higher for Unix and PHP 4.2.3 for Win32. PHP 4.3.5 starts checking the utf-8 validity of patterns.


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!