How can I match all the “special” chars (like +_*&^%$#@!~
) except the char -
in PHP?
I know that \W
will match all the “special” chars including the -
.
Any suggestions in consideration of Unicode letters?
$ means "Match the end of the string" (the position after the last character in the string).
In other words, square brackets match exactly one character. (a-z0-9) will match two characters, the first is one of abcdefghijklmnopqrstuvwxyz , the second is one of 0123456789 , just as if the parenthesis weren't there. The () will allow you to read exactly which characters were matched.
The period ( . ) is a wildcard character in regular expressions. It will match any character except a newline ( \n ).
[^-]
is not the special character you want[\W]
are all special characters as you know[^\w]
are all special characters as well - sounds fair?So therefore [^\w-]
is the combination of both: All "special" characters but without -
.
You can try this pattern
([^a-zA-Z-])
This should match all characters that are not a-z
and the -
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