normalizeDateSeparators function
Removes OCR-introduced spaces around date separators and digit clusters.
Implementation
String normalizeDateSeparators(String line) {
if (line.isEmpty) {
return line;
}
String value = line.replaceAllMapped(
RegExp(r'(?<=\d)\s*([./-])\s*(?=\d)'),
(Match match) => match.group(1) ?? '',
);
value = value.replaceAllMapped(RegExp(r'(?<=\d)\s*,\s*(?=\d)'), (_) => ',');
// Collapse spaces around dots between digits and alphanumeric characters.
value = value.replaceAllMapped(
RegExp(r'(?<=\d)\s*\.\s*(?=[A-Za-z0-9])'),
(_) => '.',
);
value = value.replaceAllMapped(
RegExp(r'(?<=[A-Za-z0-9])\s*\.\s*(?=\d)'),
(_) => '.',
);
// Collapse split numeric runs like "1 2 3" only when there are 2+ joins
// and all digit groups are single digits (fragmented number, not a column
// of distinct numbers).
final RegExp splitDigits = RegExp(r'(?<=\d)\s+(?=\d)');
final int joins = splitDigits.allMatches(value).length;
if (joins >= _digitJoinMinCount && _allSingleDigitGroups(value)) {
value = value.replaceAll(splitDigits, '');
}
return value;
}