有问题的正则表达式:
$reg = '/[.]{1,}['/'']/';
if(preg_match($reg, $dir))...
我已经在 http://regexpal.com 上测试了这个表达式,http://regex.larsolavtorvik.com/它工作正常,但在我的 PHP 脚本中我收到了这个通知。
Message: preg_match() [function.preg-match]: Compilation failed: missing terminating ] for character class at offset 12
我弄乱了"''"的数字,但它没有改变任何东西。关于可能出现什么问题的任何建议?
我试图寻找类似的问题,但我似乎遇到的只是没有添加分隔符的人。
那是因为 PHP 会将''
转义为单个'
,这将使preg_match
评估您的模式
/[.]{1,}['/']/
要在 PHP 字符串中有 2 个反斜杠,您需要实际键入 4:
$reg = '/[.]{1,}['/'''']/';
preg_match($reg, "test");
或者使用 PHP 的 heredoc:
$reg = <<<REGEX
/[.]{1,}['/'''']/
REGEX;
preg_match($reg, "test");
编辑:似乎 Heredoc 还需要 4 个反斜杠。那是因为像'n
这样的控制角色。