Parsing PHP regular expression modifier _ PHP Tutorial

Source: Internet
Author: User
Tags php regular expression
Parse the PHP regular expression modifier. We will encounter modifiers in PHP Regular Expression Learning. so what do we need to know about PHP regular expression modifiers and how to use them? So let's take a look at it. we will encounter modifiers in PHP Regular Expression Learning. so what do we need to know about PHP regular expression modifiers and how to use them? Let's take a look at its concept and related content. Before learning the PHP regular expression modifier, we should first understand the greedy mode. we mentioned above in the metacharacters "? "Another important role is" greedy mode ". what is" greedy mode?

PHP regular expression greedy mode:

For example, we want to match the string ending with the letter "a" and the letter "B", but the string to be matched contains many "B" after "", for example, "a bbbbbbbbbbbbbbbbb", will the regular expression match the first "B" or the last "B? If you use the greedy mode, it will match the last "B", and vice versa, it will only match the first "B ".

PHP regular expression greedy mode example:

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

Examples that do not use greedy mode are as follows:

 
 
  1. /a.+b/

The above uses a modifier U. for details, refer to the introduction to modifiers.

PHP regular expression modifier:

Modifiers in PHP regular expressions can change many features of regular expressions, making them more suitable for your needs (note: modifiers are case sensitive, this means that "e" is not equal to "E ").

PHP regular expression modifier types and descriptions:

◆ I: if "I" is added to the modifier, the regular expression will be case insensitive, that is, "a" and "A" are the same.

◆ M: The default regular start "^" and end "$" only if "m" is added to the modifier of the regular string ", the start and end operations refer to each line of the string. each line starts with "^" and ends with "$ ".

◆ S: if "s" is added to the modifier, the default "." indicates that any character except the line break will become any character, that is, include a line break!

◆ X: if this modifier is added, the blank characters in the expression will be ignored unless it has been escaped.

◆ E: This modifier is only useful for replacement, which indicates that it is used as PHP code in replacement.

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

◆ E: opposite to "m". if this modifier is used, "$" matches the end of an absolute string instead of the line break. this mode is enabled by default.

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

The related content of PHP regular expression modifier will be introduced here, I hope to help you understand and master PHP regular expression modifier.


Why? Let's take a look at it...

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.