Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for words containing letters in alphabetic order JAVA [duplicate]

Tags:

java

regex

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
like image 259
Yoda Avatar asked Jan 15 '13 17:01

Yoda


People also ask

How do you match letters in regex?

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.

How do you check if a string contains all the letters of the alphabet?

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.

What does \\ mean in Java regex?

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.

How do you check if a string contains all of the letters in the alphabet Java?

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).


2 Answers

^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

like image 58
Kailua Bum Avatar answered Nov 02 '22 23:11

Kailua Bum


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();
like image 37
Marko Topolnik Avatar answered Nov 03 '22 00:11

Marko Topolnik