简体   繁体   中英

Regular Expression for checking Character and Number

I have a string say, AAA123 (3 Characters followed with 3 Numbers) I want an regular expression, that actually matches this pattern. I have tried this so far,

Regex.Match(stringToBeMatched, @"\d{2}[a-zA-Z]").Success;

But its not helping, any help would be good.

It depends on what do you actually mean by matches this pattern :

  1. @"^AAA123$" - exactly AAA123 , no alternatives
  2. @"^[AZ]{3}[0-9]{3}$" - three capital letters A..Z followed by 3 digits
  3. @"^[A-Za-z]{3}[0-9]{3}$" - three letters (A..Z a..z) followed by 3 digits

To test I suggest Regex.IsMatch which looks more convenient than your version.

if (Regex.IsMatch(stringToBeMatched, @"^[A-Za-z]{3}[0-9]{3}$")) {
  //TODO: put a relevant code here
}

All you need is three alfa and three numbers? [a-zA-Z]{3}[0-9]{3}

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