java - Regex check to see if a String contains non digit fails -


why fail?

string n = "h107"; if (n.matches("\\d+")) {   system.out.println("non digit in it"); } 

i had night sleep on it, , still not it. got solution now:

if (n.matches(".*\\d+.*")) { 

but in (maybe lack of knowledge) first 1 should match. cause if has match complete string, what's point of '^' character line beginning.

that recurring problem of .matches(): misnamed. not regex matching. , problem other languages have fallen prey misnaming (python 1 example).

the problem try match whole input.

use pattern, matcher , .find() instead (.find() does real regex matching, ie find text matches anywhere in input):

private static final pattern nondigit = pattern.compile("\\d");  // in code if (nondigit.matcher(n).find())     // there non digit 

you should in fact use pattern; string's .matches() recompile pattern each time. pattern compiled once.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -