简体   繁体   中英

How to use quantifier in regular expressions?

I'm trying to enter a string without any numbers with a minimum of 4 characters but when I enter a string without numbers it still gives me an error and I don't know why.

Attempt

string pattern = "^[a-zA-Z]{4}$";
            Regex rgx = new Regex(pattern);
            string name = "evvFss";
            if(!rgx.IsMatch(name))
            {
              throw new Exception("error");
            }
            else
            {
               Console.WriteLine("correct");
            }

It shouldn't give an error because the string doesn't contain any numbers.

{4} tells it to match exactly that many characters, not at least that many.

Use {4,} to make it a minimum of four characters.

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