简体   繁体   English

正则表达式以两个字母字符开头,然后是任何数字

[英]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以两个 [A-Za-z] 字符开头
  2. ends with any number of digits以任意位数结尾

my code is:我的代码是:

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

but it returns False.但它返回 False。 Where am I wrong?我哪里错了? I've already tested the same regex with the same input on regexstorm.net and it works.我已经在regexstorm.net上使用相同的输入测试了相同的正则表达式并且它有效。

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. Regex.IsMatch 方法返回布尔值。 Please, try with this way.请用这种方式试试。 You can also try with using "$"您也可以尝试使用“$”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM