如何在PHP中匹配感叹号(!)?

时间:2012-03-13 09:42:45

标签: php regex

我想匹配以!开头的骆驼套词,例如!RedHat中包含的$line。我正在使用php 5.3.10-1ubuntu2 with Suhosin-Patch (cli)

我正在尝试以下事项:

  • $line = preg_replace(" !([A-Z])", " $1", $line);
    • 结果:PHP Warning: preg_replace(): No ending delimiter '!' found
  • $line = preg_replace(" \!([A-Z])", " $1", $line);
    • 结果:PHP Warning: preg_replace(): Delimiter must not be alphanumeric or backslash
  • $line = preg_replace(" [!]([A-Z])", " $1", $line);
    • 结果:PHP Warning: preg_replace(): Unknown modifier '('
  • $line = preg_replace(" [\!]([A-Z])", " $1", $line);
    • 结果:PHP Warning: preg_replace(): Unknown modifier '('

在PHP regexp中匹配!的正确方法是什么?

1 个答案:

答案 0 :(得分:-1)

试试这个:

!\b([A-Z][a-z]*){2,}\b

实时预览: http://regexr.com?30a95 (检查全局和多线)。