Php-pcre Regular expression pattern modifiers

Source: Internet
Author: User
Tags modifiers
The PCRE modifiers currently available are listed below. The names mentioned in parentheses are the names of these modifiers inside PCRE. The space in the pattern modifier, the newline character is ignored, and the other characters cause an error.

I (pcre_caseless)

If this modifier is set, the letters in the pattern are case insensitive and match.

M (pcre_multiline)

By default, PCRE believes that the target string is made up of one-line characters (however, it may actually contain multiple lines), and that the "line at the beginning" metacharacters (^) matches only the beginning of the string, while the "end of Row" metacharacters ($) match only the end of the string, or the last newline character (unless the D modifier is set This behavior is the same as Perl. When this modifier is set, the beginning and end of line matches any newline character in the target string before or after it, and also matches the first and last position of the target string, respectively. This is equivalent to the/m modifier of Perl. If there is no "\ n" character in the target string, or if ^ or $ is not present 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. If there is no modifier, the dot number does not match the line break. This modifier is equivalent to the/s modifier in Perl. An inverse character class such as [^a] always matches a newline character, and does not depend on the modifier's settings.

X (pcre_extended)

If this modifier is set, blank data characters in the pattern that are not escaped or are not in the character class are always ignored, and the characters between the # character and the next line break outside of an escaped character class are ignored. This modifier is equivalent to the/x modifier in Perl, which allows comments to be included in the compiled mode. Note: This is used only for data characters. Whitespace characters still cannot appear in a special sequence of characters in a pattern, such as a sequence (?) (the introduction of a conditional subgroup, which causes a compilation error if white space characters appear in a special character sequence defined by this syntax.) such as (? ( will cause errors).

E (Preg_replace_eval)

If this modifier is set, Preg_replace () is replaced with a back reference to the replacement string, executes the replacement string as a PHP code evaluation (the Eval function), and uses the execution result as the string that actually participates in the substitution. Single quotes, double quotes, backslashes (\), and NULL characters are escaped with a backslash when the back reference is replaced.


Note: Make sure that the replacement parameter is made up of a valid PHP code string, otherwise PHP will generate an explanatory error on the line called Preg_replace (). Only Preg_replace () uses this modifier, and the other PCRE function ignores this modifier.

A (pcre_anchored)

If this modifier is set, the pattern is coerced to "anchored" mode, which means that the constraint matches the 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 for Perl to implement this pattern.

D (pcre_dollar_endonly)

If this modifier is set, the meta-character dollar sign in the pattern matches only the end of the target string. If this modifier is not set, when the string ends with a newline character, the dollar sign also matches the newline character (but does not match any previous newline character). If the modifier m is set, this modifier is ignored. There is no modifier equivalent to this modifier in Perl.

S

When a pattern needs to be used more than once, it is worth taking some time to perform some additional analysis in order to get a match speed boost. If this modifier is set, this additional analysis will be performed. Currently, this analysis of a pattern only applies to non-anchored pattern matching (that is, there is no separate fixed-start character).

U (Pcre_ungreedy)

This modifier reverses the "greedy" pattern of quantifiers. Make quantifiers default to non-greedy, followed by quantifiers? Way to make it greedy. This is not compatible with Perl. It can also be set using the in-mode modifier (? U), or mark its non-greedy (e.g.. *) with a question mark after the quantifier.


Note: In non-greedy mode, characters that exceed pcre.backtrack_limit are usually not matched.

X (Pcre_extra)

This modifier opens the PCRE and Perl incompatible attachments feature. Any backslash in a pattern that Ingen a character with no special meaning causes an error that preserves these characters to ensure backward compatibility. By default, in Perl, a backslash immediately follows a character with no special meaning that is considered the original of the character. There are currently no other attributes controlled by this modifier.

J (pcre_info_jchanged)

Internal option settings (? J) Modify the local pcre_dupnames option. Allow subgroups to have the same name, (note: Only the internal option is set, the external/j setting generates an error.) )

U (PCRE_UTF8)

This modifier opens an additional feature that is incompatible with Perl. The pattern string is considered to be utf-8. This modifier is available from the UNIX version of PHP 4.1.0 or higher, Win32 version of PHP 4.2.3. PHP 4.3.5 starts checking the utf-8 legitimacy of the pattern.

  • Related Article

    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.