0
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());

This is a regex for italian tax code. It should works, I've also tried on regex101.com and it gives no error: See also here The problem is that when I run the code the result is always false. What did I do wrong? Thanks in advance

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
  • Yes, your regex *pattern* works. Regex delimiters and the modifiers are not part of the pattern, and you should provide the pattern as the argument to the Regex constructor. Use `new Regex(@"^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$", RegexOptions.IgnoreCase)` – Wiktor Stribiżew Aug 19 '16 at 12:21
  • BTW, regex101 does not support .NET regex. – Wiktor Stribiżew Aug 19 '16 at 12:23
  • Also make sure to test your regular expressions on a site that actually uses C# like http://regexstorm.net/ – juharr Aug 19 '16 at 12:24

1 Answers1

1

Just remove surrounding / from your regex, there is no need for it in .NET.

Case insensitivity can be specified using RegexOptions.IgnoreCase second argument of Regex constructor.

Regex rgx = new Regex(@"^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$", RegexOptions.IgnoreCase);
Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71