On the modifiers and i and is and s and isU in PHP regular expression

  • 2021-07-21 08:06:21
  • OfStack

Before learning about PHP regular expression modifiers, let's understand the greedy pattern, which was mentioned in metacharacters earlier. "There is also an important role, that is," greedy mode ". What is" greedy mode "?

PHP Regular Expression Greedy Pattern:

For example, if we want to match a string that starts with the letter "a" and ends with the letter "b", but the string that needs to be matched contains many "b" after "a", such as "a bbbbbbbbbbbbbbbbb", will the regular expression match the first "b" or the last "b"? If you use greedy mode, it will match the last "b", otherwise it will only match the first "b".

PHP Regular Expression Greedy Mode uses an example:

/a.+?b/
/a.+b/U

Examples of comparing non-greedy patterns are as follows:

/a.+b/

One modifier U is used above, as described in the description of modifiers.

What are the PHP regular expressions/i,/is,/s,/isU, and so on?

i is case insensitive

The dot metacharacter (.) in s pattern matches all characters, including newline characters

Whitespace characters in x mode are completely ignored except those escaped or in character classes, and all characters between # and the next 1 newline character except unescaped character classes, including both ends, are also ignored

A (PCRE_ANCHORED) If this modifier is set, the pattern is forced to be "anchored", which forces a match only from the beginning of the target string and automatically adds ^ to the beginning of the pattern.

D (PCRE_DOLLAR_ENDONLY) If this modifier is set, the dollar metacharacter in the pattern matches only the end of the target string. Without this option, if the last 1 character is a newline character, the dollar sign will also match before this character (but not before any other newline characters). If the m modifier is set, this option is ignored. There is no equivalent modifier in Perl. S When a pattern is to be used several times, it is worth analyzing first in order to speed up matching. If this modifier is set, additional analysis will be performed. Currently, parsing 1 pattern is only useful for non-anchored patterns without a single 1 fixed start character.

U (PCRE_UNGREEDY) This modifier reverses the value of the number of matches so that it is not the default duplicate, but follows the "?" It becomes repetitive. This is not compatible with Perl. You can also set (? U) modifier to enable this option.

X (PCRE_EXTRA) This modifier enables one additional feature in PCRE that is not compatible with Perl. Any backslash in the pattern followed by a letter with no special meaning causes an error, thus preserving the combination for future expansion. By default, like Perl 1, a backslash followed by a letter with no special meaning is regarded as the letter itself. No other attributes are currently controlled by this modifier. That is, greedy mode, which matches such as:/a [\ w] + to the maximum extent? e/U matches abceade in abceadeddd instead of abce and matches abce u without U correction (PCRE_UTF8) This correction enables an additional feature in PCRE that is not compatible with Perl. The pattern string is treated as UTF-8. This correction is available from PHP 4.1. 0 under Unix and from PHP 4.2. 3 under win32.

Understanding of PHP regular expression modifiers:

Modifiers in PHP regular expressions can change many of the features of the regular expression to make it more suitable for your needs (note: modifiers are case sensitive, which means "e" is not equal to "E").

Types and introduction of PHP regular expression modifiers:

i: If "i" is added to the modifier, the regularity will remove case sensitivity, i.e. "a" and "A" are identical.

m: Default regular start "^" and end "$" Only for regular strings if "m" is added to the modifier, the start and end will refer to every 1 line of the string: every 1 line begins with "^" and ends with "$".

s: If you add "s" to the modifier, the default "." means that any character except the newline character will become any character, that is, including the newline character!

x: If this 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 stands for the PHP code in replacement.

A: If this modifier is used, the expression must be the beginning of the matching string. For example, "/a/A" matches "abcd".

E: Contrary to "m", if you use this modifier, "$" matches the end of the absolute string, not before the newline character, and this mode is turned on by default.

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

PHP regular expression modifiers related content to you here, I hope you understand and master PHP regular expression modifiers are helpful.


Related articles: