Php Regular Expression modifier usage example details, php instance details

Source: Internet
Author: User
Tags php regular expression

Php Regular Expression modifier usage example details, php instance details

This example describes the php Regular Expression modifier usage. We will share this with you for your reference. The details are as follows:

<? Php // mark out of the entire mode; // example: // $ mode = "/\ bis \ B/U", where U is out; // modifier: I case-insensitive match; // For example, "/abc/I" can match abc, aBC, or ABc; // modifier: m treats the string as multiple rows, both of them can match; // example: // The mode is $ mode = "/abc/m"; // the string to be matched is: $ str = "bcefg5e \ nabcdfe" // note that \ n is replaced with a new line. abc is replaced with the next line. // $ str and $ mode can still match, the modifier m makes it possible to match multiple rows. // The modifier s treats the string as a single line and the linefeed as a common character. // example: // The pattern is: $ mode = "/pr. y/"; // the string to be matched is: $ str =" pr \ ny "; // the two cannot match ;. it is a character that can be matched except for line breaks. // The mode is changed to: $ mode = "/pr. y/s "; // Where the modifier s treats \ n as a common character, that is, it is not a line break; // The last two can be matched; // modifier: x ignores the white space in the mode; // modifier: A forcibly matches from the beginning of the target string; // example: // $ mode = "/abc/A"; // It can match $ str = "abcsdfi", // It cannot match $ str2 = "sdsdabc; // because $ str2 does not start with abc; // modifier: D if you use $ to limit the end character, line breaks are not allowed at the end; // example: // mode: $ mode = "/abc $/"; // It can match the $ str = "adshabc \ n" with the last line break; // The metachild $ ignores the last line break \ n; // If the mode is: $ mode = "/abc/D ", // It cannot match $ str = "adshabc \ n". // The modifier D cannot contain line breaks. It must end with abc. // The modifier: U only matches the nearest string. It does not repeat. // example: // The mode is $ mode = "/. * c/"; $ str =" abcabbbcab Bbbbc "; preg_match ($ mode, $ str, $ content); echo $ content [0]; // output: abcabbbcabbbbbc; // If $ mode ="/. * c/"; changed to $ mode ="/. * c/U "; // only matches the latest string, and the output is abc; // modifier: e used with the preg_replace () function, // The matching string can be executed as a regular expression.?>

Modifier:

POSIXCompatible Regular Expressions Do not have modifiers.

Possible modifiers in PERL-Compatible Regular Expressions (space and line feed in the modifier are ignored, and other characters may cause errors ):

I(PCRE_CASELESS ):
Case Insensitive for matching.

M(PCRE_MULTILINE ):
When this modifier is set, in addition to matching the beginning and end of the entire string, the row start (^) and the row end ($) also match the line break (\ n) respectively) and before.

S(PCRE_DOTALL ):
If this modifier is set, the dot metacharacters (.) In the pattern match all characters, including line breaks. If this parameter is not set, line breaks are not included.

X(PCRE_EXTENDED ):
If this modifier is set, the white space characters in the mode are ignored in addition to escape characters or in the character class.

E:
If this modifier is set, preg_replace () replaces the reverse reference in the replacement string as a normal replacement, evaluate it as the PHP code, and use the result to replace the searched string. This modifier is only used by preg_replace (), which is ignored by other PCRE functions.

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

D(PCRE_DOLLAR_ENDONLY ):
If this modifier is set, the row end ($) in the pattern matches only the end of the target string. Without this option, if the last character is a line break, it will also be matched. If the m modifier is set, ignore this option.

S:
When a mode is used several times, it is worth analyzing for acceleration matching. If this modifier is set, additional analysis is performed. Currently, the analysis mode is only useful for non-anchored modes without a single fixed start character.

U(PCRE_UNGREEDY ):
Make "?" Is greedy by default.

X(PCRE_EXTRA ):
Any backslash followed by a letter with no special meaning in the pattern causes an error, so that this combination is retained for future expansion. By default, a backslash followed by a letter without special meaning is treated as the letter itself.

U(PCRE_UTF8 ):
The modulo string is treated as a UTF-8.

Note:

Pattern Modifiers)

I-Uppercase and lowercase letters can be matched simultaneously.
M-Treat strings as multiple rows
S-The string is treated as a single line, and the line break is treated as a common character so that "." matches any character.
X-The white space in the mode is ignored.
U-Match the nearest string
E-Use the replaced string as an expression

PS: here we will provide two very convenient Regular Expression tools for your reference:

JavaScript Regular Expression online testing tool:
Http://tools.jb51.net/regex/javascript

Regular Expression generation tool:
Http://tools.jb51.net/regex/create_reg

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.