注意:STREETTYPES和QUADRANT常量分别包含所有相关的街道类型和象限.
我在这里添加了一个子集:
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,Quadrant,Streetname和StreetType都是该类的属性.
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; } } } }解决方法 我不知道你在哪个国家,但是如果你在美国,想要花费一些钱来进行地址验证,你可以购买相关的usps产品 here.而 here是找到免费的单词列表的好地方usps用于预期的单词和缩写.我相信其他国家可以使用类似的网页. 总结
以上是内存溢出为你收集整理的c# – 解析邮件地址的正则表达式全部内容,希望文章能够帮你解决c# – 解析邮件地址的正则表达式所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)