What are the modifiers in the PHP regular expression?

Source: Internet
Author: User
Tags modifiers php regular expression
Before learning the PHP regular expression modifier, first to understand the greedy mode, before the meta-character mentioned in the "?" There is also an important role, namely "greedy mode", what is "greedy mode"?

PHP Regular Expression 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 :

/a.+?b/
/a.+b/u

The comparison of instances that do not use greedy mode is as follows:

/a.+b/

The above uses a modifier u, see the description of the modifier.

PHP Regular expression/I,/is,/s,/isu etc. what are these things?

I case-insensitive

S-mode dot-character (.) matches all characters, including line breaks

X-mode whitespace characters in addition to being escaped or completely ignored outside of the character class, outside of the escaped character class # And all characters between the next newline character, including both ends, are also ignored

A (pcre_anchored) If this modifier is set, the pattern is forced to "anchored", that is, forcing the match to start only from the beginning of the target string and automatically adds ^ at the beginning of the pattern.

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

U (pcre_ungreedy) modifier Reverses the value of the matched quantity so that it is not the default repetition, and then it is followed by "?". only to become repetitive. This is not compatible with Perl. You can also set the (? U) modifier to enable this option.

X (pcre_extra) This modifier enables an additional feature that is incompatible with Perl in a PCRE. 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. That is: Greedy mode, the maximum match, such as:/a[\w]+?e/u match abceadeddd in Abceade instead of ABCE, if not plus u correction, then the match ABCE u (PCRE_UTF8) This modifier enables a PCRE in an additional feature that is 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.

the understanding of the PHP regular expression modifier :

Modifiers in a regular expression in PHP can change many of the regular features, making regular expressions more appropriate for your needs (note: Modifiers are sensitive to capitalization, which means "E" is not equal to "E").

The type and description of the PHP regular expression modifier:

I: If you add "I" to the modifier, the regular will remove the case sensitivity, i.e. "a" and "a" are the same.

M: Default regular start "^" and end "$" just for regular strings if you add "M" to the modifier, then the start and end will refer to each line of the string: the beginning of each line is "^" and the End is "$".

S: If "s" is added to the modifier, then the default "." Any character that represents anything other than a newline character will become any characters, including line breaks!

x: If the modifier is added, the white space character in the expression will be ignored unless it has been escaped.

E: This modifier is only useful for replacement, and represents the PHP code in replacement.

A: If you use this modifier, the expression must be the beginning of the matching string. For example, "/a/a" matches "ABCD".

E: In contrast to "M", if this modifier is used, then "$" will match the end of the absolute string, not the line break, which is turned on by default.

U: Similar to question mark, used to set "greedy mode".

The relevant contents of the PHP regular expression modifier are introduced to you here, hoping to help you understand and master the PHP regular expression modifiers.

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.