Regex to match any character including new lines

后端 未结 4 1818
深忆病人
深忆病人 2020-11-28 04:39

Is there a regex to match \"all characters including newlines\"?

For example, in the regex below, there is no output from $2 because (.+?)

相关标签:
4条回答
  • 2020-11-28 05:01

    If you don't want add the /s regex modifier (perhaps you still want . to retain its original meaning elsewhere in the regex), you may also use a character class. One possibility:

    [\S\s]
    

    a character which is not a space or is a space. In other words, any character.

    You can also change modifiers locally in a small part of the regex, like so:

    (?s:.)
    
    0 讨论(0)
  • 2020-11-28 05:01

    Yeap, you just need to make . match newline :

    $string =~ /(START)(.+?)(END)/s;
    
    0 讨论(0)
  • 2020-11-28 05:01

    You want to use "multiline".

    $string =~ /(START)(.+?)(END)/m;
    
    0 讨论(0)
  • 2020-11-28 05:17

    Add the s modifier to your regex to cause . to match newlines:

    $string =~ /(START)(.+?)(END)/s;
    
    0 讨论(0)
提交回复
热议问题