I want to match camel cased words beginning with ! such as !RedHat contained in $line. I’m using php 5.3.10-1ubuntu2 with Suhosin-Patch (cli).
I’m trying following things:
$line = preg_replace(" !([A-Z])", " $1", $line);- result:
PHP Warning: preg_replace(): No ending delimiter '!' found
- result:
$line = preg_replace(" \!([A-Z])", " $1", $line);- result:
PHP Warning: preg_replace(): Delimiter must not be alphanumeric or backslash
- result:
$line = preg_replace(" [!]([A-Z])", " $1", $line);- result:
PHP Warning: preg_replace(): Unknown modifier '('
- result:
$line = preg_replace(" [\!]([A-Z])", " $1", $line);- result:
PHP Warning: preg_replace(): Unknown modifier '('
- result:
How is the correct way to match ! in PHP regexp?
You need to use delimiters in your regex – non-alphanumeric, as the error message states:
Notice the
/characters at the beginning and end of the regex string.These don’t have to be
/– you could use#or even!– but if you use!then you’ll need to escape the!char inside the regex itself.