This is not a good problem for regex, it is almost certainly easier to check it in a for loop:
import static java.lang.Character.isLetter; import static java.lang.Character.toLowerCase; public boolean alphabeticalOrder(String word) { for (int i = 0; i < word.length() - 1; i++) { if (!isLetter(word.charAt(i)) || toLowerCase(word.charAt(i + 1)) < toLowerCase(word.charAt(i))) return false; } return isLetter(word.charAt(word.length() - 1)); }
Alternatively, the following regular expression will also match correctly (only for ASCII alphabetic characters) when passed to CASE_INSENSITIVE :
^a*b*c*d*e*f*g*h*i*j*k*l*m*n*o*p*q*r*s*t*u*v*w*x*y*z*$
source share