<?php
$string = 'test';
echo preg_match('/te(?# comments)st/', $string) . "\n";
echo preg_match('/te#~~~~
st/', $string) . "\n";
echo preg_match('/te#~~~~
st/x', $string) . "\n";
// result
// 1
// 0
// 1
コメントは、(?# という並びにより始まり、次の閉じカッコで終わります。 カッコのネストはできません。コメント内の文字は、パターンマッチには 全く関係しません。
PCRE_EXTENDED オプションが設定されている場合は、パターン中の文字クラス外にある エスケープされていない # 文字からもコメントが始まり、 次の改行文字で終わります。
<?php
$string = 'test';
echo preg_match('/te(?# comments)st/', $string) . "\n";
echo preg_match('/te#~~~~
st/', $string) . "\n";
echo preg_match('/te#~~~~
st/x', $string) . "\n";
// result
// 1
// 0
// 1