Tengo una clase de dirección que usa una expresión regular para analizar el número de la casa, el nombre de la calle y el tipo de calle desde la primera línea de una dirección. Este código generalmente funciona bien, pero estoy publicando aquí para compartir con la comunidad y para ver si alguien tiene sugerencias para mejorar.Expresión regular para analizar direcciones postales
Nota: Las constantes STREETTYPES y QUADRANT contienen todos los tipos de calle y cuadrantes relevantes, respectivamente.
He incluido un subconjunto aquí:
private const string STREETTYPES = @"ALLEY|ALY|ANNEX|AX|ARCADE|ARC|AVENUE|AV|AVE|BAYOU|BYU|BEACH|...";
private const string QUADRANTS = "N|NORTH|S|SOUTH|E|EAST|W|WEST|NE|NORTHEAST|NW|NORTHWEST|SE|SOUTHEAST|SW|SOUTHWEST";
HouseNumber, Cuadrante, StreetName y StreetType son todas las propiedades de la clase.
private void Parse(string line1)
{
HouseNumber = string.Empty;
Quadrant = string.Empty;
StreetName = string.Empty;
StreetType = string.Empty;
if (!String.IsNullOrEmpty(line1))
{
string noPeriodsLine1 = String.Copy(line1);
noPeriodsLine1 = noPeriodsLine1.Replace(".", "");
string addressParseRegEx =
@"(?ix)
^
\s*
(?:
(?<housenumber>\d+)
(?:(?:\s+|-)(?<quadrant>" +
QUADRANTS +
@"))?
(?:(?:\s+|-)(?<streetname>\S+(?:\s+\S+)*?))??
(?:(?:\s+|-)(?<quadrant>" +
QUADRANTS + @"))?
(?:(?:\s+|-)(?<streettype>" + STREETTYPES +
@"))?
(?:(?:\s+|-)(?<streettypequalifier>(?!(?:" +
QUADRANTS +
@"))(?:\d+|\S+)))?
(?:(?:\s+|-)(?<streettypequadrant>(" +
QUADRANTS + @")))??
(?:(?:\s+|-)(?<suffix>(?:ste|suite|po\sbox|apt)\s*\S*))?
|
(?:(?:po|postoffice|post\s+office)\s+box\s+(?<postofficebox>\S+))
)
\s*
$
";
Match match = Regex.Match(noPeriodsLine1, addressParseRegEx);
if (match.Success)
{
HouseNumber = match.Groups["housenumber"].Value;
Quadrant = (string.IsNullOrEmpty(match.Groups["quadrant"].Value)) ? match.Groups["streettypequadrant"].Value : match.Groups["quadrant"].Value;
if (match.Groups["streetname"].Captures.Count > 1)
{
foreach (Capture capture in match.Groups["streetname"].Captures)
{
StreetName += capture.Value + " ";
}
StreetName = StreetName.Trim();
}
else
{
StreetName = (string.IsNullOrEmpty(match.Groups["streetname"].Value)) ? match.Groups["streettypequalifier"].Value : match.Groups["streetname"].Value;
}
StreetType = match.Groups["streettype"].Value;
//if the matched street type is found
//use the abbreviated version...especially for credit bureau calls
string streetTypeAbbreviation;
if (StreetTypes.TryGetValue(StreetType.ToUpper(), out streetTypeAbbreviation))
{
StreetType = streetTypeAbbreviation;
}
}
}
}
Centrándose en la separación de la parte número de casa del resto, esto es demostrado (Direcciones alemanes) código de producción utilicé: '/ (. *) [\.] ((?: \ d {1,4} -)? \ d {1,4} (?: \ s? \ w)?) $/i' it captura "foostreet 42", "foost.14" (espacio omitido), "footst. 14-22" (intervall), "footstreet 14 A" (letra calificada/w o sin espacio antes), sin embargo, s resistente a la mayoría de otros insumos (es decir si es parcialmente datos heredados, con información en el lugar equivocado, etc.). –