Skip to main content

Simple script to help diagnose Regex faulure

Symptoms​

You have a string which is inexplicably failing to match a regular expression. Example: an MRZ read from passport.

Cause​

It may be the case that the characters you see in the string are not the same 'simple' characters you expect. "А" looks identical to "A" but one is ASCII 65 and the other a Cyrillic character which looks the same.

The code below was used to demonstrate this result:

Resolution​

You may need to implement a character substitution on your string. To help diagnose the problem a script such as the one below might help. We use [https://dotnetfiddle.net/](https://dotnetfiddle.net/) for quickly running similar scripts.

using System; using System.Text.RegularExpressions;

public class Program { public static void Main() { Regex regex = new Regex(@"^[A-Z0-9]+$", RegexOptions.IgnoreCase); string input = "АBC123"; var matchResult = regex.Match(input); if (matchResult.Success) { Console.WriteLine("Successful match"); foreach (Group m in matchResult.Groups) { Console.WriteLine($"{m.Name}: {m.Value}"); } } else { Console.WriteLine("Could not match, is there a problem with the ASCII character codes?"); for (int i = 0; i = '0' && character = 'A' && character