PHP Regular expression pattern modifier _php Tutorial

Source: Internet
Author: User
Tags character classes php regular expression
Modifier is an important reference to the regular expression in PHP, let me turn to you about the PHP expression pattern modifier in detail, there is a need to know the friend can enter the reference.

The PHP pattern modifier, also known as the pattern modifiers, is used outside the delimiter of the regular expression. Mainly used to adjust the interpretation of regular expressions, to extend the regular expression in the matching, substitution and other operations of some functions, enhance the ability of the regular. But there are a lot of local explanations are wrong, it is easy to mislead others, so today I put this document out for your reference.

Pattern Correction Symbol Function description

I in and regular match is case insensitive
M treats a string as multiple lines. The default regular start "^" and End "$" Use the target note string as a single "line" character (even including newline characters). If you add "M" to the modifier, the start and end of each line that will be pointing to the string begins with "^" Ending and "$".
If the modifier is set, the matched string will be treated as a line, including a newline character, and the newline character will be treated as a normal string.
x ignores whitespace, unless escaping is not ignored.
E is used only in the Preg_replace () function, where the inverse reference in the replacement string is a normal substitution, evaluates it as a PHP code, and replaces the searched string with its result.
A If you use this modifier, the expression must be the beginning of the matching string. For example, "/a/a" matches "ABCD".
The $ character Fu Quan in D mode matches the end of the target character. Without this option, if the last character is a line break, the dollar sign will also match this character. If modifier m is set, this is ignored.
E in contrast to "M", if this modifier is used, "$" will match the end of the absolute string instead of the newline character, which is turned on by default.
U greedy mode, similar to the role of the question mark, the maximum match is greedy mode.

Greedy mode:

For example, we want to match the letter "a" at the beginning of the letter "B" end of the string, but need to match the string after "a" contains a lot of "B", such as "a bbbbbbbbbbbbbbbbb", that the regular expression will match the first "B" or the Last "B"? If you use greedy mode, it will match to the last "B", and vice versa only to the first "B".

PHP Regular Expression Greedy pattern usage example:

1./a.+?b/
2./a.+b/u
The comparison of instances that do not use greedy mode is as follows:

1./a.+b/
The above uses a modifier u, see the description of the modifier.

Other information:

Pattern modifier: Explains the modifier used in the regular expression pattern

Description: The following list of modifiers that are currently possible in PCRE. The internal PCRE names of these modifiers are in parentheses. Spaces and line breaks in the modifier are ignored, and other characters cause errors.

I (pcre_caseless)

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

M (pcre_multiline)

By default, PCRE the target string as a single "line" character (even if it contains a line break). The line start metacharacters (^) match only the beginning of the string, and the "Line End" metacharacters ($) only match the end of the string, or the last character is preceded by a newline (unless the D modifier is set). This is the same as Perl.
When this modifier is set, line start and line end match the beginning and end of the entire string, respectively, after and before the newline character. This is equivalent to Perl's/M modifier. If there is no "n" character in the target string or there is no ^ or $ 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. Without this setting, the line break is not included. This is equivalent to Perl's/s modifier. Exclude character classes such as [^a] always match line breaks, regardless of whether this modifier is set.

X (pcre_extended)

If this modifier is set, the whitespace characters in the pattern are ignored, in addition to being escaped or completely ignored outside of the character class, and all characters, including both ends, of the # and the next newline character outside the non-escaped characters class. This is equivalent to Perl's/x modifier, which allows annotations to be added to complex schemas. Note, however, that this applies only to data characters. Whitespace characters may never appear in a special sequence of characters in a pattern, such as a sequence that introduces conditional sub-patterns (? (middle).

E

If this modifier is set, Preg_replace () makes a normal substitution of the inverse reference in the replacement string, evaluates it as a PHP code, and replaces the searched string with its result.
Only Preg_replace () uses this modifier, and the other PCRE function ignores it.
Note: This modifier is not available in PHP3.

A (pcre_anchored)

If this modifier is set, the pattern is coerced to "anchored", which forces the match to start only from the beginning of the target string. This effect can also be implemented by the appropriate pattern 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 this character before (but not before any other line break). This option is ignored if the M modifier is set. There is no equivalent modifier in Perl.

S

When a pattern is to be used several times, it is worth analyzing it to speed up the match. If this modifier is set, additional analysis is performed. Currently, parsing a pattern is only useful for non-anchored patterns that do not have a single fixed starting character.

U (Pcre_ungreedy)

This modifier reverses the value of the matched quantity so that it is not the default repetition, and becomes followed by "?". only to become repetitive. This is not compatible with Perl. You can also set the (? U) modifier or enable this option after the quantifier with a question mark (such as. *?).

X (Pcre_extra)

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

U (PCRE_UTF8)

This modifier enables additional features in a PCRE that are incompatible with Perl. The pattern string is treated as UTF-8. This modifier is available under Unix from PHP 4.1.0 and is available under Win32 from PHP 4.2.3. starting from PHP 4.3.5 Check the UTF-8 legitimacy of the mode.


Basic syntax for PHP regular expressions:

A regular expression, divided into three parts: delimiters, expressions, and modifiers.

The delimiter can be any character other than a special character (such as "/!"). And so on), the commonly used delimiter is "/". Expressions are made up of special characters (see below for special characters) and non-special strings, such as "[a-z0-9_-]+@[a-z0-9_-.] + "can match a simple e-mail string. Modifier is used to turn on or off a function/mode. Here is an example of a complete regular expression:

/hello.+?hello/is above the regular expression "/" is the delimiter, two "/" between the expression, the second "/" after the string "is" is the modifier.

If you have delimiters in an expression, you need to use the escape symbol "", such as "/hello.+?/hello/is". Escape symbols can also execute special characters in addition to delimiters, and all the special characters that are composed of letters need to be "" to escape, such as "D" for all numbers.

http://www.bkjia.com/PHPjc/628791.html www.bkjia.com true http://www.bkjia.com/PHPjc/628791.html techarticle modifier is an important reference to the regular expression in PHP, let me turn to you about the PHP expression pattern modifier in detail, there is a need to know the friend can enter the reference ...

  • 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.