I am sure that this was asked earlier, but I cannot find it (or I know the correct wording for the search)
Basically I want a regular expression that matches all non-alphanumeric except hyphens. So basically match \ W + except exclude '-' I'm not sure how to exclude certain from the finished set.
\W is short for [^\w] . So:
\W
[^\w]
[^\w-]+
A bit of background:
[…]
[^…]
\v
[[:alpha:]]
[[:alnum:]]
will do just that. Match any non- \w characters except for a hyphen.
\w
You can use:
[^a-zA-Z0-9_-]
or
[^\w-]
to match a single non-hyphen, not an alphanumeric. To match one or more prefix with +
+
In Java7 or later, you need to add (?U) to match all language characters. eg.
(?U)
(?U)[^\w-]
In the Java string (you need to print the \ character with another):
\
(?U)[^\\w-]