Talking about modifiers in PHP regular expressions/I,/is,/s,/isu_php tips

Source: Internet
Author: User
Tags modifier modifiers php regular expression

To understand the greedy pattern before learning the PHP regular expression modifier, before the word "?" in the meta character. There is also an important role in the "greedy model", what is the "greedy model"?

PHP Regular Expression greedy mode :

Like we're going to match a string that ends with the letter "a" at the beginning of the letter "B", but the string that needs to be matched has a lot of "B" after "a", such as "a bbbbbbbbbbbbbbbbb", and that regular expression matches 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

Examples of comparisons that do not use greedy patterns are as follows:

/a.+b/

a modifier u is used above, as described in the introduction to modifiers.

PHP Regular Expression/I,/is,/s,/isuWhat are all these things?

I do not case sensitive

The dot meta character (.) in S mode matches all characters, including line breaks

whitespace characters in x mode are completely ignored in addition to being escaped or outside of the character class, and all characters, including both ends, that are not escaped from the character class and the next line break are ignored

A (pcre_anchored) if this modifier is set, the pattern is coerced to "anchored", that is, forcing only the start of a match from the beginning of the target string is automatically added ^ 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 newline, the dollar sign also matches the character before it (but does not match any other line breaks). This option is ignored if the M modifier is set.  There is no equivalent modifier in Perl. S when a pattern is used several times, it is worthwhile to analyze it for the sake of accelerated matching. If this modifier is set, an 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 matching number of values so that it is not the default repetition, and becomes followed by the "?" to become repetitive. This is not compatible with Perl. It can also be set in the pattern (? 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, thus preserving this combination for future expansion. By default, as with Perl, a backslash is followed by a letter that has no special meaning. No other features are currently controlled by this modifier. That is: Greedy mode, maximum matching, such as:/a[\w]+?e/u matching abceadeddd in the abceade instead of ABCE, if you do not add a correction, then match ABCE u (PCRE_UTF8) This modifier enables an additional feature that is incompatible with Perl in PCRE. 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 inside PHP Regular expressions 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, it will remove the case sensitivity, that is, "a" and "a" are the same.

M: The default regular start "^" and end "$" just for regular strings if you add "M" to the modifier, the start and end will refer to each line of the string: "^" at the beginning of each line, and "$" at the end.

S: If you add "s" to the modifier, the default "." Any character that represents anything other than a line break will become any character, including a newline character!

X: If you add the modifier, 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 "$" matches the end of the absolute string, rather than the newline character, which is turned on by default.

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

PHP Regular expression modifier related content to introduce you here, I hope you understand and grasp the PHP regular expression modifiers help.

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.