Published 21 Aug, 2022

Java - Java regex: check if word has non alphanumeric characters

Category Java
Modified : Oct 04, 2022
58

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 ??

Answers

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.

28

Change your regex to:

.*\\W+.*

42

Your expression does not take account of possible non-English letters. It's also more complicated than it needs to be. Unless you are using regexs for some reason other than need (such as your professor having told you to) you are much better off with:

boolean found = false;
for (int i=0;i<mystring.length();++i) {
  if (!Character.isLetterOrDigit(mystring.charAt(i))) {
    found=true;
    break;
  }
}

36

Methods are in the wrong case.

The matcher was declared as m but used as matcher.

The repetition should be "one or many" + instead of "zero or many " * This works correctly:

String term = "Hello-World";
boolean found = false;
Pattern p = Pattern.compile("\\W+");//<-- compile( not Compile(
Matcher m = p.matcher(term);  //<-- matcher( not Matcher
if(m.find()) {  //<-- m not matcher
    found = true;
}

Btw, it would be enough if you just :

boolean found = m.find();

:)


29

It's 2016 or later and you should think about international strings from other alphabets than just Latin. The frequently cited [^a-zA-Z] will not match in that case. There are better ways in Java now:

[^\\p^\\p]

See the reference (section "Classes for Unicode scripts, blocks, categories and binary properties"). There's also this answer that I found helpful.