Simple script to help diagnose Regex faulure

Created by Jeremy Burgess, Modified on Fri, 22 Dec, 2023 at 1:22 PM by Jeremy Burgess

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/ 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 < input.Length; i++)
      {
        char character = input[i];
        int asciiCode = (int)character;
        string result = "";
        if ((character >= '0' && character <= '9') || (character >= 'A' && character <= 'Z') || character == '<')
        {
          result = "OK";
        }
        else
        {
          result = "!!";
        }

        Console.WriteLine($"{result} Position: {i}, ASCII Code: {asciiCode}, Character: {character}");
      }
    }
  }
}

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article