Regular Expression - 2 letters and 2 numbers in C#

后端 未结 3 1266
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-03 22:30

I am trying to develop a regular expression to validate a string that comes to me like: \"TE33\" or \"FR56\" or any sequence respecting 2 letters and 2 numbers.

The

3条回答
  •  滥情空心
    2021-01-03 23:11

    You're missing an ending anchor.

    if(Regex.IsMatch(myString, "^[A-Za-z]{2}[0-9]{2}\z")) {
        // ...
    }

    Here's a demo.


    EDIT: If you can have anything between an initial 2 letters and a final 2 numbers:

    if(Regex.IsMatch(myString, @"^[A-Za-z]{2}.*\d{2}\z")) {
        // ...
    }

    Here's a demo.

提交回复
热议问题