Java - Java regex: check if word has non alphanumeric characters
This is my code to determine if a word contains any non-alphanumeric characters:
String term = "Hello-World"; boolean found = false; Pattern p = Pattern.Compile("\\W*"); Matcher m = p.Matcher(term); if(matcher.find()) found = true;
I am wondering if the regex expression is wrong. I know
"\W" would matches any non-word characters. Any idea on what I am missing ??
There are 4 suggested solutions here and each one has been listed below with a detailed description. The following topics have been covered briefly such as Regex, Java. These have been categorized in sections for a clear and precise explanation.