Valid: abc
abc11
Invalid: 11
a-b
a&&b
a << b
a&b
a->b
Valid: abc
abc11
Invalid: 11
a-b
a&&b
a << b
a&b
a->b
You need two regexes. One that checks it's only alphanumeric /[a-zA-Z0-9]+/
and one that checks it's not just digits /^[^0-9]+$/
Only check the second one if the first one passes.
This regexp should do the job: /^[a-zA-Z\d]*[a-zA-Z][a-zA-Z\d]*^/
Mind you, this regex is potentially going to a significant amount of backtracking; it will be O(N^2)
in the worst case. Making the first repetition lazy instead of eager will make the regex faster ... if it is going to match. The way to speed up the no-match case is to use two regexes.
Or better still, don't use regexes at all and code match in simple Java:
public boolean matchSymbol (String input) {
boolean seenLetter = false;
final int len = input.length();
for (int i = 0; i < len; i++) {
char ch = input.charAt(i);
if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') {
seenLetter = true;
} else if (ch < '0' || ch > '9') {
return false;
}
}
return seenLetter;
}
For an N character String, this succeeds in N
loop iterations or fails in (typically) less than N
iterations.