PHP regular match All characters fail reason and solution

Source: Internet
Author: User
Tags php regular expression preg
This article is mainly to share with you the PHP regular match all characters failure reasons and solutions, hope to help everyone.

<?php$str = ' @@@@i (pcre_caseless) If this modifier is set, the letters in the pattern are case insensitive to match. @@@@m (Pcre_multiline) ';//regular 1: $preg = '/@@@@ (. *) @@@@/'; Cannot match line break \n//regular 2: $preg = '/@@@@ (. *) @@@@/s ';//\s matches any whitespace character,   equivalent to [\f\n\r\t\v]//\s   match any characters except whitespace, equivalent to [^\f\n\r\t\v ]preg_match_all ($preg, $str, $matches);p Rint_r ($matches);//Result 1:array (    [0] = = Array ()    [1] = = Array ())// The result 2:array (    [0] = = Array        (            [0] = @@@@i (pcre_caseless) If this modifier is set, the letters in the pattern are case insensitive to match.    1 = = Array            0 = i (pcre_caseless) if this modifier is set, the letters in the pattern are case insensitive.        ))

PHP Regular Expression modifiers:

  • I: If this modifier is set, the letters in the pattern will be case insensitive to match.

  • M: By default, pcre that the target string is made up of single-line characters (however it may actually contain more than one line), and that the "beginning of the line" metacharacters (^) matches only the beginning of the string, and the "end of Row" metacharacters (

    , setting this modifier has no effect.

  • S: If this modifier is set, the dot character in the pattern matches all characters, including the line break. If there is no modifier, the dot number does not match the line break. This modifier is equivalent to the/s modifier in Perl. An inverse character class such as [^a] always matches a newline character, and does not depend on the modifier's settings.

  • x: If this modifier is set, blank data characters in the pattern that are not escaped or are not in the character class are always ignored, and the characters between the # character and the next line break outside of an escaped character class are ignored. This modifier is equivalent to the/x modifier in Perl, which allows comments to be included in the compiled mode. Note: This is used only for data characters. Whitespace characters still cannot appear in a special sequence of characters in a pattern, such as a sequence (? A conditional subgroup was introduced (this syntax defines a special character sequence in which a white-space character causes a compilation error.) such as (? ( will cause errors.).

  • E: If this modifier is set, Preg_replace () is replaced with a back reference to the replacement string, executes the replacement string as a PHP code evaluation (the Eval function), and uses the execution result as the string that is actually participating in the substitution. Single quotes, double quotes, backslashes (), and null characters are escaped with a backslash when the back reference is replaced.

    Tip Make sure that the replacement parameter is made up of a valid PHP code string, otherwise PHP will generate an explanatory error on the line that Preg_replace () calls. Note: only Preg_replace () uses this modifier, and the other pcre function ignores this modifier.
  • A: If this modifier is set, the pattern is forced to "anchored" mode, which means that the constraint matches the search only from the beginning of the target string. This effect can also be constructed using the appropriate pattern, and this is the only way for Perl to implement this pattern.

  • D: If this modifier is set, the meta-character dollar sign in the pattern matches only the end of the target string. If this modifier is not set, when the string ends with a newline character, the dollar sign also matches the newline character (but does not match any previous newline character). If the modifier m is set, this modifier is ignored. There is no modifier equivalent to this modifier in Perl.

  • S: When a pattern needs to be used multiple times, it is worth taking some time to perform some additional analysis in order to get a match speed boost. If this modifier is 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: This modifier reverses the "greedy" mode of quantifiers. To make quantifiers default to non-greedy, they can be greedy by the way they are followed by quantifiers. This is not compatible with Perl. It can also be set using the in-mode modifier (? U), or mark its non-greedy (e.g.. *) with a question mark after the quantifier.

    Note: In non-greedy mode, characters that exceed pcre.backtrack_limit are usually not matched.
  • X: This modifier opens the Pcre and Perl incompatible attachments feature. Any backslash in a pattern that Ingen a character with no special meaning causes an error that preserves these characters to ensure backward compatibility. By default, in Perl, a backslash immediately follows a character with no special meaning that is considered the original of the character. There are currently no other attributes controlled by this modifier.

  • J: Internal option settings (? J) Modify the local pcre_dupnames option. Allow child groups to have duplicate names. (Note: The external/j setting generates an error only through the internal option setting.)

  • U: This modifier opens an additional feature that is incompatible with Perl. The pattern string is considered to be utf-8. This modifier is available from the UNIX version of PHP 4.1.0 or higher, Win32 version of PHP 4.2.3. PHP 4.3.5 starts checking the utf-8 legitimacy of the pattern.

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.