简体   繁体   中英

Regex start with exactly two alphabetic characters and than followed by any digits

I'm trying to create a very simple regex that accepts strings:

  1. starts with two [A-Za-z] characters
  2. ends with any number of digits

my code is:

Regex.IsMatch("AA00000000000", $"^[A-Za-z]{2}[0-9]*$")

but it returns False. Where am I wrong? I've already tested the same regex with the same input on regexstorm.net and it works.

Working for me with below code:

string pattern = @"^[A-Za-z]{2}[0-9]*$";
string str = "AA00000000000";
bool val = Regex.IsMatch(str, pattern);

你在正则表达式前面放了一个“$”,因为“{2}”部分将被解释为“2”,所以被评估的正则表达式看起来像这样:“^[A-Za-z ]2[0-9]*$”。

Regex.IsMatch method return boolean value. Please, try with this way. You can also try with using "$"

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