PHP Regex Any Character

后端 未结 5 641
醉梦人生
醉梦人生 2020-12-10 01:10

The . character in a php regex accepts all characters, except a newline. What can I use to accept ALL characters, including newlines?

相关标签:
5条回答
  • 2020-12-10 01:27

    The PHP Manual page for Dot states that:

    If the PCRE_DOTALL option is set, then dots match newlines as well.

    0 讨论(0)
  • 2020-12-10 01:28

    An important thing is missing here. [\s\S] matches one character, whereas a newline can be a character sequence. (Windows uses two characters: \r\n.) Neither . (with DOT_ALL modifier) nor [\s\S] will match the newline sequence. Best way to match any character or any newline is (.|\R), "everything except a newline or a newline". \R matches \n, \r and \r\n.

    0 讨论(0)
  • 2020-12-10 01:31

    It's the the . character that means "every character" (edit: OP edited). And you need to add the option s to your regexp, for example :

    preg_match("`(.+)`s", "\n");
    
    0 讨论(0)
  • 2020-12-10 01:38

    This is commonly used to capture all characters:

    [\s\S]
    

    You could use any other combination of "Type-X + Non-Type-X" in the same way:

    [\d\D]
    [\w\W]
    

    but [\s\S] is recognized by convention as a shorthand for "really anything".

    You can also use the . if you switch the regex into "dotall" (a.k.a. "single-line") mode via the "s" modifier. Sometimes that's not a viable solution (dynamic regex in a black box, for example, or if you don't want to modify the entire regex). In such cases the other alternatives do the same, no matter how the regex is configured.

    0 讨论(0)
  • 2020-12-10 01:40

    would

    [.\n]+
    

    not work?

    How about (.|\n)+? I tested it and it seems to work.

    I am quite sure this is the literal interpretation of exactly what you were asking for.

    0 讨论(0)
提交回复
热议问题