Skip to content

Instantly share code, notes, and snippets.

@savaged
Created March 31, 2023 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save savaged/ba502eaa0c956d10eb755df302c02abc to your computer and use it in GitHub Desktop.
Save savaged/ba502eaa0c956d10eb755df302c02abc to your computer and use it in GitHub Desktop.
Some regex to clean OCR 'O' for zero mistakes in UK postcodes
using System.Text.RegularExpressions;
foreach (var postcode in args)
Console.WriteLine(postcode.ReplaceAnyMisplacedO());
static class OcrPostCodeCleaningExtensions
{
public static string ReplaceAnyMisplacedO(this string postcode) =>
postcode.ReplaceAnyFrontO().ReplaceAnyEndO();
private static string ReplaceAnyFrontO(this string postcode) =>
Regex.Replace(postcode, @"^(?<start>[A-Z]{1,2}[0-9])O(?<end>\s?(O|[0-9])[A-Z]{2})$", "${start}0${end}");
private static string ReplaceAnyEndO(this string postcode) =>
Regex.Replace(postcode, @"^(?<start>[A-Z]{1,2}[0-9]{1,2})\s?O(?<end>[A-Z]{2})$", "${start} 0${end}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment