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);
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 '('
How is the correct way to match !
in PHP regexp?
It's a boolean tester. Empty or false. Show activity on this post. It's the not boolean operator, see the PHP manual for further detail.
! If an exclamation mark (!) occurs as the first character in a regular expression, all magic characters are treated as special characters. An exclamation mark is treated as a regular character if it occurs anywhere but at the very start of the regular expression.
In programming and scripting languages, the exclamation mark is used as "not." For example, "! =" also means not equal. See our operator definition for further information. Used to help identify a nonexecutable statement.
You need to use delimiters in your regex - non-alphanumeric, as the error message states:
$line = preg_replace("/ !([A-Z])/", " $1", $line);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With