Haritha Madhushanka
Haritha Madhushanka

Reputation: 257

flutter regex to find two lowercase letters with any number of Uppercase letters, digits and symbols

I am new to regular expressions. I want to write a regex to detect two lowercase letters in a String. The String could contain any number of UpperCase letters, digits and symbols. Just want to make sure that the String contains two lowercase letters for sure using the regex expression.

I tried using

RegExp(r'[a-zA-Z]{2}')

But it returns true even when the String contains 1 lowercase and 1 uppercase. I want to check if the String contains at least 2 lowercase letters.

Could someone help?

Upvotes: 2

Views: 2500

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627419

You can use a regex like

(?:[^a-z]*[a-z]){2}

See the regex demo. Details:

  • (?: - start of a non-capturing group:
    • [^a-z]* - zero or more chars other than lowercase ASCII letters
    • [a-z] - a lowercase ASCII letter
  • ){2} - end of the grouping construct, match two repetitions

In Dart, you can use

int n = 2;
RegExp rex = RegExp("(?:[^a-z]*[a-z]){$n}");
print(rex.hasMatch("Food")); // => true

Upvotes: 1

Related Questions