Pattern modifier _ PHP Tutorial

Source: Internet
Author: User
Pattern modifier. Pattern modifier-describe the modifier description used in regular expression pattern. The following lists the modifiers that may be used in PCRE. Contains the internal pattern modifiers for these modifiers.

Pattern modifier-describe the modifier used in regular expression pattern

Description

The following lists the delimiters that may be used in PCRE. The inner PCRE names of these modifiers are in brackets.

I(PCRE_CASELESS)

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

M(PCRE_MULTILINE)

By default, PCRE uses the target string as a single "line" character (or even contains a line break ). The line start metacharacters (^) only match the start of the string, and the line end metacharacters ($) only match the end of the string, or when the last character is a line break (unless it is setDModifier ). This is the same as Perl.

When this modifier is set, "row start" and "row end" not only match the start and end of the entire string, but also match the end and end of the linefeed respectively. This is equivalent to the/m modifier of Perl. If the target string does not contain the \ n character or the mode does not contain ^ or $, this modifier is set to no effect.

S(PCRE_DOTALL)

If this modifier is set, the DOT metacharacters (.) in the pattern match all characters, including line breaks. If this parameter is not set, line breaks are not included. This is equivalent to the/s modifier of Perl. For example, [^ a] always matches a line break, regardless of whether this modifier is set.

X(PCRE_EXTENDED)

If this modifier is set, the white space characters in the mode are ignored except for escaped characters or in the character class, all the characters between the # and the next line break except the unescaped character class, including both ends, are ignored. This is equivalent to the/x modifier of Perl, so that annotations can be added in complex modes. However, note that this only applies to data characters. A blank character may never appear in a special character sequence in a pattern, for example, a sequence that introduces a condition subpattern (? (Middle.

E

If this modifier is set,Preg_replace ()In the replacement string, the reverse reference is replaced normally, and it is evaluated as the PHP code, and the searched string is replaced with the result.

OnlyPreg_replace ()This modifier is ignored by other PCRE functions.

Note:This modifier is unavailable in PHP3.
Example: modify all html tags to uppercase.
Preg_replace ("/(<\/?) (\ W +) ([^>] *>)/e "," '\ 1 '. strtoupper ('\ 2 '). '\ 3' ", $ html_body );
However, it is reported that this process is 20 times slower than the preg_replace_callback method.

A(PCRE_ANCHORED)

If this modifier is set, the pattern is forced to "anchored", that is, it is forced to match only from the beginning of the target string. This effect can also be achieved through the appropriate mode 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 before this character (but not before any other line breaks ). IfMThe modifier ignores this option. Perl does not have an equivalent modifier.

S

When a mode is used several times, it is worth analyzing for acceleration matching. If this modifier is set, additional analysis is performed. Currently, the analysis mode is only useful for non-anchored modes without a single fixed start character.

U(PCRE_UNGREEDY)

This modifier reverses the value of the matching quantity so that it is not the default repetition, but becomes followed by "?" . This is incompatible with Perl. You can also set in the mode (? U) to enable this option.

X(PCRE_EXTRA)

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

U(PCRE_UTF8)

This modifier enables additional features that are not compatible with Perl in a PCRE. The pattern string is treated as a UTF-8. This modifier is available in Unix from PHP 4.1.0 and win32 from PHP 4.2.3.
Example:
Preg_match ('/[\ x {2460}-\ x {2468}]/U', $ str); match Chinese characters with internal codes

Parser pattern modifier-describe the modifier description used in regular expression pattern. The following lists the delimiters that may be used in PCRE. Inside the brackets are the corrections...

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.