PHP Regular Modifier Usage example analysis

Source: Internet
Author: User
This article mainly introduced the PHP regular modifier usage, combined with the example form the PHP regular modifier's function, the use method and the related attention matters, the need friend can refer to the following

This example describes the PHP regular modifier usage. Share to everyone for your reference, as follows:

<?php//Mark out of the entire pattern;  Example://$mode = "/\bis\b/u", where U is outside;     Modifier: I do not match case-by-case;  such as: "/abc/i" can be matched with ABC or ABC or ABC;  Modifier: M treats the string as multiple lines, regardless of whether the line can match;     Example://mode: $mode = "/abc/m";     The string to match is: $str = "BCEFG5E\NABCDFE"//note where \ n, newline; ABC changed to the next line;  $str and $mode can still be matched, and modifier m makes multiple lines match;   Modifier: s treats a string as a single line, and a newline character as a normal characters;      Example://mode: $mode = "/pr.y/";      To match the string to: $str = "Pr\ny"; The two cannot be matched;.      is to match the characters except the newline;        The modified mode is: $mode = "/pr.y/s";      where modifier s will be \ n treated as ordinary characters, that is, not newline;  The last two can be matched;  Modifier: x ignores whitespace in the pattern;   Modifier: A forces A match from the beginning of the target string;      Example://$mode = "/abc/a";      Can match with $str= "ABCSDFI",//Can not match with $str2= "SDSDABC";  Because $STR2 does not begin with ABC; Modifier: D  If you use the limit end character, the trailing line is not allowed;      Example://mode: $mode = "/abc$/";      It can be matched with the $str= "adshabc\n" with the last line of newline;      Yenzi $ will ignore the last line break \ n;  If the mode is: $mode = "/ABC/D",//Can not match $str= "adshabc\n",//modifier D to limit its non-line, must end with ABC;  Modifier: U matches only the nearest string;      Example://If the mode is: $mode = "/a.*c/";      $str = "ABCABBBCABBBBBC";      Preg_match ($mode, $str, $content); echo $content [0]; Output: ABCABBBCABBBBBC;      If $mode= "/a.*c/", Turn $mode= "/a.*c/u"; Matches only the last string, output: abc;//modifier: E mate function preg_replace () use,//can be matched to the string as a regular expression execution; >

Modifier:

POSIX compliant regular no modifier.

Possible modifiers used in Perl-compatible regularization (spaces in the modifier and line breaks are ignored, other characters cause errors):

i (pcre_caseless):
ignores case when matching.

m (pcre_multiline):
When this modifier is set, the line start (^) and line end ($) match the beginning and end of the entire string, respectively, after and before the line break (\ n).

s (pcre_dotall):
If this modifier is set, the dot character (.) in the pattern matches all characters, including the line break. Without this setting, the line break is not included.

x (pcre_extended):
If this modifier is set, white-space characters in the pattern are completely ignored, except those that are escaped or are not in the character class.

e :
If this modifier is set, Preg_replace () makes a normal substitution of the inverse reference in the replacement string, evaluates it as a PHP code, and replaces the searched string with its result. Only Preg_replace () uses this modifier, and the other PCRE function ignores it.

A (pcre_anchored):
If this modifier is set, the pattern is coerced to "anchored", which forces the match to start only from the beginning of the target string.

D (pcre_dollar_endonly):
If this modifier is set, the end of the line in the pattern ($) matches only the end of the target string. Without this option, if the last character is a newline, it will be matched. This option is ignored if the M modifier is set.

S :
When a pattern is to be used several times, it is worth analyzing it to speed up the match. If this modifier is set, additional analysis is performed. Currently, parsing a pattern is only useful for non-anchored patterns that do not have a single fixed starting character.

U (pcre_ungreedy):
Make "?" The default match becomes a greedy state.

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

u (PCRE_UTF8):
The pattern string is treated as UTF-8.

Attention:

Pattern modifier (Pattern Modifiers)

I -can match uppercase and lowercase letters
M -Treats a string as multiple lines
S -treats the string as a single line, and the newline character is treated as ordinary characters, so that "." Match any character
X -whitespace in the pattern is ignored
U -Match to the nearest string
e -use the replaced string as an expression

The above is the whole content of this article, I hope that everyone's study has helped.


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.