Home  >  Article  >  Backend Development  >  Parsing PHP regular expression modifiers_PHP tutorial

Parsing PHP regular expression modifiers_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:26:45833browse

We will encounter modifiers in the study of PHP regular expressions, so what should we pay attention to in understanding and using PHP regular expression modifiers? So let’s take a closer look at its concept and related content. 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, we want to match strings starting with the letter "a" and ending with the letter "b", but we need to match the string There are many "b"s after "a", such as "a bbbbbbbbbbbbbbbbb". Will the regular expression 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:

<ol class="dp-c">
<li class="alt"><span><span>/a.+?b/   </span></span></li>
<li><span>/a.+b/U </span></li>
</ol>

Comparison of examples without using greedy mode is as follows:

<ol class="dp-c"><li class="alt"><span><span>/a.+b/  </span></span></li></ol>

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

Understanding of PHP regular expression modifiers:

The modifiers in PHP regular expressions can change many characteristics of regular expressions, making regular expressions more suitable for you (Note: modifiers are case-sensitive, meaning "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, i.e. "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: The beginning of each line is "^" and the end is "$".

◆s: If "s" is added to the modifier, the default "." means that any character except 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.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446585.htmlTechArticleWe will encounter modifiers in the study of PHP regular expressions, so about PHP regular expression modifiers What do we need to pay attention to when understanding and using it? So let’s take a look at it in detail...
Statement:
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