Modifiers of PHP regular expressions_PHP tutorial

WBOY
Release: 2016-07-13 10:34:09
Original
869 people have browsed it

When using regular expressions in PHP, there will be some differences from other languages. One of the differences is that PHP has modifiers (qualifiers) for regular expressions.

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, 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:

  1. /a.+?b/
  2. /a.+b/U

Comparison of examples without using greedy mode is as follows:

  1. /a.+b/

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

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: the beginning of each line It is "^" and ends with "$".
  • s: If "s" is added to the modifier, the default "." representing 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/752344.htmlTechArticleWhen using regular expressions in PHP, there will be some differences from other languages. One of the differences is that PHP There are modifiers (qualifiers) for regularity. We are in PHP regular expression...
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