Regex match everything between two string, spaning multiline

Solution 1:

Check the dotall checkbox in RegExr :)

Without the dotall flag (the s in /regex/s), a dot (.) won't match carriage returns.

You should use .*? instead of .* to lazy match the optional content (see the PLEASE DO NOT MATCH! sentence in the examples).

Solution 2:

To make a regex ungreedy, use a ? after the *:

<!-- OPTIONAL -->(.*?)<!-- OPTIONAL END -->

Does this help you?

Also depending on your programming language you use, you have modifiers that will make your regex dot (.) match newlines too. For PHP you have the s (dotall) modifier for example:

http://php.net/manual/en/reference.pcre.pattern.modifiers.php