8

/(<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
  • 14,307
  • 21
  • 67
  • 92

3 Answers3

16

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
  • 144,265
  • 20
  • 237
  • 291
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,529
  • 1
  • 21
  • 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
  • 55,977
  • 11
  • 154
  • 194