How to match with regex all special chars except "-" in PHP?

CaTz picture CaTz · Mar 15, 2012 · Viewed 43.7k times · Source

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?

Answer

hakre picture hakre · Mar 15, 2012
  • [^-] 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 -.