My student(high school) asked me a question about regex matching a word which contains letters in alphabetic order. To be honest I do not know how to create regex such that. Example of words matching, size of letters does not matter:
abc, aWZ, gOR, bor
Using character sets For example, the regular expression "[ A-Za-z] " specifies to match any single uppercase or lowercase letter. In the character set, a hyphen indicates a range of characters, for example [A-Z] will match any one capital letter.
Read the String. Convert all the characters in the given String to lower case using the toLower() method. Convert it into a character array using the toCharArray() method of the String class. Find whether every character in the array is in between a and z, if not, return false.
Backslashes in Java. The backslash \ is an escape character in Java Strings. That means backslash has a predefined meaning in Java. You have to use double backslash \\ to define a single backslash. If you want to define \w , then you must be using \\w in your regex.
To check if String contains only alphabets in Java, call matches() method on the string object and pass the regular expression "[a-zA-Z]+" that matches only if the characters in the given string is alphabets (uppercase or lowercase).
^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*$
should work
This should work:
(?i)a*b*c*...z*
It would be easy to construct in a loop.
StringBuilder b = new StringBuilder(64);
b.append("(?i)");
for (int i = 'a'; i <= 'z'; i++) b.append((char)i).append('*');
return Pattern.compile(b.toString()).matcher(input).matches();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With