Detailed description of Regular Expression Pattern modifiers in PHP

Source: Internet
Author: User
Tags email string php regular expression

Modifier is an important reference for regular expressions in php. Next I will explain the Regular Expression Pattern modifier in PHP, for more information, see references.

The PHP pattern modifier, also known as the pattern modifier, is used outside the delimiters of regular expressions. It is mainly used to adjust the interpretation of regular expressions, and extends some functions of Regular Expressions in operations such as matching and replacement, and enhances the ability of regular expressions. However, many explanations are incorrect and easy to mislead others. So today I will sort out this document for your reference.

Description of the pattern correction symbol function

I is case insensitive to regular expression matching.
M treats the string as multiple rows. The default regular start "^" and end "$" treat the target string as a single "line" character (or even a line break ). If "m" is added to the modifier, the start and end of each line of the string start with "^" and end with "$ ".
S if this modifier is set, the matched string will be regarded as a line, including a line break, and the line break will be considered as a normal string.
X ignores the blank space, unless the escape is not ignored.
E only uses the preg_replace () function to reverse reference the replacement string for normal replacement. Use it as the PHP code to evaluate the value and use the result to replace the searched string.
If A uses this modifier, the expression must be the beginning of the matched string. For example, "/a/A" matches "abcd ".
In D mode, the $ character weight matches the end of the target character. Without this option, if the last character is a line break, the dollar sign will also match before this character. If the modifier m is set, ignore this option.
E is opposite to "m". If this modifier is used, "$" matches the end of the absolute string instead of the line break. This mode is enabled by default.
The U greedy mode has the same effect as the question mark. The best match is the greedy mode.

Greedy mode:

For example, we want to match the string ending with the letter "a" and ending with the letter "B", but the string to be matched contains many "B" after "", for example, "a bbbbbbbbbbbbbbbbb", will the regular expression match the first "B" or the last "B? If you use the greedy mode, it will match the last "B", and vice versa, it will only match the first "B ".

PHP Regular Expression greedy mode example:

1./a. +? B/
2./a. + B/U
Examples that do not use greedy mode are as follows:

1./a. + B/
The above uses a modifier U. For details, refer to the introduction to modifiers.

Other materials:

Pattern modifier: Describe the modifier used in regular expression pattern.

Note: The delimiters that may be used in pcre are listed below. The inner PCRE names of these modifiers are in brackets. The space and line feed in the modifier are ignored. Other characters may cause errors.

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 the last character is before the line break (unless the D modifier is set ). 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 () replaces the reverse reference in the replacement string as a normal replacement, evaluate it as the PHP code, and use the result to replace the searched string.
This modifier is only used by preg_replace (), which is ignored by other PCRE functions.
Note: This modifier is unavailable in PHP3.

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 ). If the m modifier is set, ignore 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) modifier or a question mark (such as. *?) after the number operator .*?) 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. Check the validity of the UTF-8 in the mode from PHP 4.3.5.


Basic PHP regular expression syntax:

A regular expression is divided into three parts: separator, expression, and modifier.

Separators can be any character except special characters (for example "/! ", Etc.), the commonly used separator is "/". The expression is composed of special characters (see the following for special characters) and non-special strings, such as "[a-z0-9 _-] + @ [a-z0-9 _-.] + "can match a simple email string. Modifier is used to enable or disable a function/mode. The following is an example of a complete regular expression:

/Hello. +? The regular expression "/" above hello/is a separator, the expression is between two "/", and the string "is" after the second "/" is a modifier.

If the expression contains delimiters, you need to use the escape symbol "", such as "/hello. +? /Hello/is ". In addition to separators, escape symbols can also execute special characters. All special characters consisting of letters must be escaped by "". For example, "d" represents all numbers.

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.