288

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

For example, in the regex below, there is no output from $2 because (.+?) doesn't include new lines when matching.

$string = "START Curabitur mollis, dolor ut rutrum consequat, arcu nisl ultrices diam, adipiscing aliquam ipsum metus id velit. Aenean vestibulum gravida felis, quis bibendum nisl euismod ut. 

Nunc at orci sed quam pharetra congue. Nulla a justo vitae diam eleifend dictum. Maecenas egestas ipsum elementum dui sollicitudin tempus. Donec bibendum cursus nisi, vitae convallis ante ornare a. Curabitur libero lorem, semper sit amet cursus at, cursus id purus. Cras varius metus eu diam vulputate vel elementum mauris tempor. 

Morbi tristique interdum libero, eu pulvinar elit fringilla vel. Curabitur fringilla bibendum urna, ullamcorper placerat quam fermentum id. Nunc aliquam, nunc sit amet bibendum lacinia, magna massa auctor enim, nec dictum sapien eros in arcu. 

Pellentesque viverra ullamcorper lectus, a facilisis ipsum tempus et. Nulla mi enim, interdum at imperdiet eget, bibendum nec END";

$string =~ /(START)(.+?)(END)/;

print $2;
kurotsuki
  • 3,927
  • 7
  • 23
  • 28

4 Answers4

391

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:.)
iliketocode
  • 6,978
  • 5
  • 46
  • 60
ephemient
  • 189,938
  • 36
  • 271
  • 385
236

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

$string =~ /(START)(.+?)(END)/s;
BoltClock
  • 665,005
  • 155
  • 1,345
  • 1,328
  • 38
    In JavaScript: (START)[\s\S]*(END) - See www.regexpal.com to test – Zymotik Jul 15 '14 at 15:40
  • 1
    For more info regarding @Zymotik's comment, see: http://stackoverflow.com/questions/1068280/javascript-regex-multiline-flag-doesnt-work – Jacob van Lingen Jul 19 '16 at 07:14
  • 2
    In Java you can use the inline modifier (?s) at the beginning of the regex, for example to replace any character including newlines after 'yourPattern' use `"(?s)yourPattern.*"`- Also see: https://www.rexegg.com/regex-modifiers.html#dotall – LukeSolar Aug 07 '19 at 13:40
  • In Ruby, the modifier is `m`, not `s`. See: https://rubular.com/ – Jon Schneider Jan 13 '20 at 23:17
  • JavaScript now supports this way. ES2018 added the `s` [dotAll](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/dotAll) flag. – Lyall Sep 28 '20 at 03:56
10

Yeap, you just need to make . match newline :

$string =~ /(START)(.+?)(END)/s;
FailedDev
  • 26,037
  • 9
  • 50
  • 71
-6

You want to use "multiline".

$string =~ /(START)(.+?)(END)/m;
BoltClock
  • 665,005
  • 155
  • 1,345
  • 1,328
nadime
  • 145
  • 6