A brief discussion on the modifiers /i, /is, /s, /isU in PHP regular expressions, regular expression isu_PHP tutorial

WBOY
Release: 2016-07-13 10:16:28
Original
970 people have browsed it

A brief discussion on the modifiers /i, /is, /s, /isU in PHP regular expressions, the regular expression isu

Before learning PHP regular expression modifiers, let’s first understand the greedy mode. As mentioned earlier in the metacharacter, "?" also has an important role, that is, "greedy mode". What is "greedy mode"? ?

PHP regular expression greedy mode :

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"s after "a", such as "a bbbbbbbbbbbbbbbbb", then the regular expression Will it match the first "b" or the last "b"? If you use greedy mode, the last "b" will be matched, otherwise only the first "b" will be matched.

Examples of using PHP regular expression greedy mode:

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

Comparison of examples without using greedy mode is as follows:

/a.+b/

A modifier U is used above, please see the introduction to modifiers for details.

What are the PHP regular expressions /i, /is, /s, /isU, etc.?

i is not case sensitive

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

Whitespace characters in the x pattern are completely ignored except those that are escaped or in a character class. All characters between # and the next newline character outside the unescaped character class, inclusive, are also ignored. Ignore

A (PCRE_ANCHORED) If this modifier is set, the pattern is forced to be "anchored", that is, it is forced to match only from the beginning of the target string, that is, ^ is automatically added to the beginning of the pattern.

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

U (PCRE_UNGREEDY) This modifier inverts the value of the match count so that it is not repeated by default, but becomes repeated when followed by a "?" This is not compatible with Perl. This option can also be enabled by setting the (?U) modifier in the mode.

X (PCRE_EXTRA) This modifier enables an extra feature in PCRE that is not compatible with Perl. Any backslash in the pattern followed by a letter with no special meaning results in an error, thus preserving this combination for future expansion. By default, like Perl, a backslash followed by a letter with no special meaning is treated as the letter itself. No other traits are currently controlled by this modifier. That is: greedy mode, maximum matching such as: /a[w]+?e/U matches abceade in abceadeddd instead of abce. If no U correction is added, it matches abce u (PCRE_UTF8) This modifier enables a PCRE Extra functionality that is incompatible with Perl. Pattern strings are treated as UTF-8. This modifier is available since PHP 4.1.0 under Unix and since PHP 4.2.3 under win32.

Understanding of PHP regular expression modifiers :

Modifiers in PHP regular expressions can change many characteristics of the regular expression, making the regular expression 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 regular expression will cancel the case sensitivity, that is, "a" and "A" are the same.

◆m: The default regular start "^" and end "$" are only for regular strings. If "m" is added to the modifier, then the start and end will refer to each line of the string: each line The beginning is "^" and the end is "$".

◆s: If "s" is added to the modifier, the default "." representing any character other than the newline character will become any character, including the newline character!

◆x: If this modifier is added, whitespace characters in the expression will be ignored unless it has been escaped.

◆e: This modifier is only useful for replacement, which means it is used as PHP code in replacement.

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

◆E: Contrary to "m", if this modifier is used, then "$" will match the absolute end of the string, not before the newline character. This mode is turned on by default.

◆U: It has the same function as the question mark, and is used to set the "greedy mode".

This concludes the introduction to PHP regular expression modifiers. I hope it will be helpful for you to understand and master PHP regular expression modifiers.

Who can help me explain what role the global modifier and g of regular expressions in PHP play? It is best to use a small example to prove it

When replacing, if there is no g, only the first matching item will be replaced, if there is, all matching items will be replaced.

php regular expression

Both single quotes and / can be used as inclusion characters in regular templates. In fact, # can also be used. This is for the convenience of pattern matching. For example, if you want to match content with single quotes, then do not use single quotes to wrap the template Include it, use / or #, this can simplify the template, otherwise, you need to add the escape character

before the single quotation mark in the template. The i at the end means case insensitive, which is not sensitive to upper and lower case, s can be understood as full-text matching, that is, if the matched content has line breaks, it is best to add this switch

In the template, \s means a space

I am an expert in regular expressions , there is a special book that teaches this, you can ask me for it, QQ1389951
Give me points haha

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/897697.htmlTechArticleA brief discussion on the modifiers /i, /is, /s, /isU and regular expressions in PHP regular expressions isu Before learning PHP regular expression modifiers, let’s first understand greedy mode. I mentioned "" in metacharacters before...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template