7

/(<pre>|<code>|\[code])(.*)(</pre>|</code>|\[/code])/gi

This works if I have something such as:

<code>foobar</code>

But if I were to have a line-break like this:

<code>
    Awesome
</code>

It will not match it, what am I doing wrong?

daryl
  • 13,529
  • 20
  • 66
  • 90

3 Answers3

15

You do need the DOTALL modifer /s, because the . dot per default excludes linebreaks. The /g modifier OTOH is not legal in PHP and PCRE.

You should also use .*? to not match too wide.

mario
  • 141,508
  • 20
  • 234
  • 284
6

In PCRE, "." does not match every character, it matches every thing that isn't a newline:

Outside a character class, a dot in the pattern matches any one character in the subject, including a non-printing character, but not (by default) newline.

(http://www.php.net/manual/en/regexp.reference.dot.php)

Try something like [\s\S] instead.

Kara Brightwell
  • 2,431
  • 1
  • 19
  • 29
3

Because . matches every character except newline by default, unless you feed in the s switch.

See explanation of regex switches here.

In particular

s (PCRE_DOTALL) If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded.

So /(<pre>|<code>|\[code])(.*)(</pre>|</code>|\[/code])/is.

(No g, use preg_match_all).

mathematical.coffee
  • 54,152
  • 10
  • 138
  • 187