Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java regex for "any symbol" that's neither word nor number nor space

Tags:

java

regex

As the title says: I want the input to be one or more symbols that is not in the union of letters, numbers and white space. So basically any of ~!@#, etc. I have

"^(?=.*[[^0-9][^\w]])(?=\\S+$)$"

I know I could negate the appropriate set, but I don't know how to create my super set to start with. Would the following do?

"^(?=.*[(_A-Za-z0-9-\\+)])(?=\\S+$)$"
like image 273
Cote Mounyo Avatar asked Feb 16 '23 23:02

Cote Mounyo


2 Answers

Maybe you're looking for \p{Punct}, which matches any of !"#$%&'()*+,-./:;<=>?@[]^_`{|}~.

String re = "\\p{Punct}+";
like image 102
arshajii Avatar answered Feb 18 '23 13:02

arshajii


The class:

[^\w\s]

This will match any non-alphanumeric/non-whitespace character.

Java String:

String regex = "[^\\w\\s]";
like image 27
Brian Avatar answered Feb 18 '23 12:02

Brian