Regex matches all sets except certain

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.

+3
source share
4 answers

\W is short for [^\w] . So:

 [^\w-]+ 

A bit of background:

  • […] defines the set
  • [^…] cancels the set
  • As a rule, each \v (smallcase) set is negated by the symbol \v (in upper case), where V is any letter that defines the set.
  • for international characters, you can see [[:alpha:]] and [[:alnum:]]
+8
source
 [^\w-]+ 

will do just that. Match any non- \w characters except for a hyphen.

+1
source

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 +

+1
source

In Java7 or later, you need to add (?U) to match all language characters. eg.

 (?U)[^\w-] 

In the Java string (you need to print the \ character with another):

 (?U)[^\\w-] 
0
source

All Articles