正则表达式返回总是假c#

时间:2016-08-19 12:18:05

标签: c# .net regex forms

Regex rgx = new Regex(@"/^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/i");

bool result = rgx.IsMatch("PPPPLT80R10M082K"); 

MessageBox.Show(result.ToString());

这是意大利税法的正则表达式。它应该有效,我也尝试过regex101.com并且没有错误: See also here 问题是当我运行代码时,结果总是错误的。我做错了什么? 提前致谢

1 个答案:

答案 0 :(得分:0)

只需从正则表达式中删除周围的/,就不需要在.NET中使用它。

可以使用RegexOptions.IgnoreCase构造函数的Regex第二个参数指定不区分大小写。

Regex rgx = new Regex(@"^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$", RegexOptions.IgnoreCase);