简体   繁体   中英

regex for a string only contains small letters

Hi I am trying to find a regEx which only accepts lower case letters nothing else,

I tired few options nothing worked, Can some one please guide me :

here are the expressions I tried :

(?=.*[a-z])--> this is not working if we have "aaaAA"

[a-z]+^[A-Z]

^[A-Z]+[a-z]+^[A-Z]

Please help me.

Did you try just [az]+

See example here: https://regex101.com/r/wCdBue/1

If you want to check if whole line / text contains only lowercase letter you should wrap your regex with ^$ like this: ^[az]+$

Example: https://regex101.com/r/T0DHY4/1

Your solution is nearly correct. The regex must say "[az]+"—include a quantifier, which means that you are not matching a single character, but one or more lowercase characters.

String regex = "[az]+";

Here is a quantifier regex link. http://docs.oracle.com/javase/tutorial/essential/regex/quant.html

It's working for me like this:

  String line = "aaaA";
  System.out.println(line.matches("^[a-z]+$")); //false
  line = "aaaa";
  System.out.println(line.matches("^[a-z]+$")); //true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM