How to check all characters in a string in lower case using java

I tried like this, but it prints false, Please help me

String inputString1 = "dfgh";// but not dFgH String regex = "[az]"; boolean result; Pattern pattern1 = Pattern.compile(regex); Matcher matcher1 = pattern1.matcher(inputString1); result = matcher1.matches(); System.out.println(result); 
+6
source share
5 answers

Your decision is almost correct. The regular expression should say "[az]+" - turn on the quantifier, which means that you do not agree with one character, but with one or more lowercase characters. Note that the uber-correct solution, which matches any lower case char in Unicode, and not just the English alphabet, is as follows:

 "\\p{javaLowerCase}+" 

Also, note that you can achieve this with much less code:

 System.out.println(input.matches("\\p{javaLowerCase}*")); 

(here I alternatively use the quantifier *, which means zero or more. Choose according to the desired semantics.)

+16
source

you are almost there, except that you check only one character.

 String regex = "[az]+"; 

the above expression will check if the input string will contain any number of characters from a to z

read how to use Quantifiers in regex

+5
source

Use this template:

 String regex = "[az]*"; 

Your current template only works if the tested string has only one char.

Note that it does exactly what it looks like: it doesn’t really check if the string is case sensitive, but if it does not contain characters outside of [az] . This means that it returns false for lowercase strings like "Γ bcd" . The correct solution in the Unicode world would be to use the Character.isLowercase () function and scroll the line.

+2
source

It should be

 ^[az]+$ 

^ - start of line

$ is the end of the line

[az]+ matches 1 to many small characters

You need to use quantitative values ​​such as * , which corresponds to 0 for many characters, + , which corresponds to 1 to many characters. They will match 0 or 1 to many times the previous character or range

+2
source

Why worry about regex?

 String inputString1 = "dfgh";// but not dFgH boolean result = inputString1.toLowerCase().equals( inputString1 ); 
0
source

All Articles