Home > Article > Backend Development > Introduction to PHP regular expressions /i, /s, /x,/u, /U, /A, /D, /S, etc.
i (PCRE_CASELESS)
If this is set Modifiers , The letters in the pattern will be matched case-insensitively .
# #m (PCRE_MULTILINE)
By default, PCREthinks that the target string is composed of single-line characters(However, in fact it May contain multiple lines ), "Start of line"metacharacter(^)matches only the beginning of the string, and"End of line"metacharacter($)Only matches the end of the string , or the last newline character (unless # is set ##Dmodifier). This behavior is the same as perl . When this modifier is set, "First of line" and"End of line" It will match , before or after any newline character in the target string. In addition, , will also match the beginning and end of the target string respectively. The last position is . This is equivalent to the /mmodifier# of perl ##. If the target string does not contain the "\n"character , or ^ or $ does not appear in the pattern, Setting this modifier does not produce any Impact.
s## (PCRE_DOTALL) If this modifier is set , The dot metacharacter in the pattern matches all characters , including newlines . If there is no modifier, dot does not match the newline character . This modifier is equivalent to the /smodifier# in perl ##.A negated character class such as [^a] always matches newline characters , does not depend on the setting of this modifier. x (PCRE_EXTENDED) If this modifier is set, the characters in the pattern are not escaped or are not in the character class Whitespace data characters in , and # characters outside an unescaped character class and the next are always ignored Characters between newlines are also ignored . This modifier is equivalent to perl# The /x modifiers and in ## enable the compiled schema to contain comments . Note: This is only used for data characters. White space characters Still cannot appear in the special character sequence of the pattern , For example, the sequence (?( introduces a conditional subgroup (Translation Note: This syntax defines If a blank character appears in the special character sequence, it will cause Compilation error. For example(
?( will result in error.). e (PREG_REPLACE_EVAL) If this modifier Set , preg_replace()After performing backreference replacement on the replacement string, Execute the replaced string as phpcode evaluation(eval function Method), and use the execution result as the string that actually participates in the replacement. Single quotes, Double quotes, Backslash(\)# The ## and NULL characters are backslash-escaped when backreference substitution. Tip replacementThe parameters consist of legal php code strings, otherwisephp will in preg_replace() An interpretation error . is generated on the calling line Note: Only preg_replace()Use this modifier, OtherPCREFunction ignores this modifier. ##A (PCRE_ANCHORED) If set With this modifier , the mode is forced to the "anchored" mode , That is to say, constrain the match so that it searches only from the beginning of the target string. This effect can also be constructed using appropriate patterns,and This is alsoperlThe only way to implement this pattern. D (PCRE_DOLLAR_ENDONLY) If this modifier is set, The metacharacter dollar sign in the pattern only matches the end of the target string. If this modifier is not set, When the string ends with a newline character, The dollar sign will also match the newline character(but will not match any previous newline character). If The modifier m is set, This modifier is ignored. in# There is no equivalent modifier to this modifier in ##perl. S When a pattern needs to be used multiple times, In order to improve the matching speed , Worth spending some time Do some additional analysis. If With this modifier , set, this additional analysis will be performed . Currently, This analysis of a pattern only applies to non-anchored pattern matching(That is, there is no separate fixed start character). U (PCRE_UNGREEDY) This modifier reverses the quantifier's "greedy"mode. Make the quantifier default to non-greedy , by following the quantifier ? Can make it greedy. This is incompatible withperl. It can also be set using Intra-mode modifier settings(?U), Or mark it as non-greedy with a question mark after the quantifier (For example, .*?). Note: In non-greedy mode, usually cannot match characters exceeding pcre.backtrack_limit . X (PCRE_EXTRA) This modifier turns on the PCRE attachment feature that is incompatible with perl. Any backslash in the pattern followed by ingen a character with no special meaning will result in an error , These characters are reserved to ensure backward compatibility. Default In the case of , in perl, the backslash is followed by one without Characters with special meaning are considered to be the original text of the character . No other properties are currently controlled by this modifier. J (PCRE_INFO_JCHANGED) ##Internal option settings(?J)Modify local PCRE_DUPNAMESoptions. Allow subgroups Duplicate name.
(Annotation:can only be set via internal options, external/JThe setting will generate an error.) u (PCRE8) This modifier opens an additional feature that is incompatible with perl. Pattern strings are considered utf-8's . This modifier from unix版php
4.1.0 or higher, win32 versionphp 4.2.3begins to be available.
php 4.3.5Start checking the patternutf-8Legality. This modifier turns on additional
functionality of PCRE that is incompatible with Perl. Pattern strings are treated as UTF-8. This modifier is available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32. UTF-8 validity of the pattern is checked since PHP 4.3.5.
The above is the detailed content of Introduction to PHP regular expressions /i, /s, /x,/u, /U, /A, /D, /S, etc.. For more information, please follow other related articles on the PHP Chinese website!