Created
March 31, 2023 14:04
-
-
Save savaged/ba502eaa0c956d10eb755df302c02abc to your computer and use it in GitHub Desktop.
Some regex to clean OCR 'O' for zero mistakes in UK postcodes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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