正则表达式将德国地址拆分为其部分 [英] Regex for splitting a german address into its parts

查看:368
本文介绍了正则表达式将德国地址拆分为其部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上好,

我正试图通过Java将德国地址字符串的各个部分拆分成它的部分。有没有人知道正则表达式或图书馆这样做?要拆分它,如下所示:

I'm trying to splitting the parts of a german address string into its parts via Java. Does anyone know a regex or a library to do this? To split it like the following:

NamederStraße25a88489 Teststadt

to < br>
NamederStraße| 25a | 88489 | Teststadt

Teststr。 3 88489 Beispielort(GroßerKreis)

to

Teststr。|​​ 3 | 88489 | Beispielort(GroßerKreis)

如果系统/正则表达式仍然有用,如果邮政编码或城市等部分丢失,那将是完美的。

It would be perfect if the system / regex would still work if parts like the zip code or the city are missing.

有没有我可以存档的正则表达式或库?

Is there any regex or library out there with which I could archive this?

编辑:德国地址规则:

街道:字符,数字和空格

房屋号码:数字和任何字符(或空格),直到一系列数字(zip)(至少在这些例子中)< br>
邮编:5位数
地方或城市:其余可能还有空格,逗号或大括号

Rule for german addresses:
Street: Characters, numbers and spaces
House no: Number and any characters (or space) until a series of numbers (zip) (at least in these examples)
Zip: 5 digits
Place or City: The rest maybe also with spaces, commas or braces

推荐答案

我遇到了类似的问题,并稍微调整了这里提供的解决方案,并找到了这个解决方案,它也有效,但(imo)有点简单易懂和扩展:

I came across a similar problem and tweaked the solutions provided here a little bit and came to this solution which also works but (imo) is a little bit simpler to understand and to extend:

/^([a-zäöüß\s\d.,-]+?)\s*([\d\s]+(?:\s?[-|+/]\s?\d+)?\s*[a-z]?)?\s*(\d{5})\s*(.+)?$/i

这里有一些示例匹配

它还可以处理丢失的街道号码,并且可以通过向字符类添加特殊字符来轻松扩展。 / p>

It can also handle missing street numbers and is easily extensible by adding special characters to the character classes.

[a-zäöüß\s\d,.-]+?                         # Street name (lazy)
[\d\s]+(?:\s?[-|+/]\s?\d+)?\s*[a-z]?)?     # Street number (optional)

之后,必须有邮政编码,这是唯一的部分是绝对必要的,因为它是唯一不变的部分。邮政编码后的所有内容都被视为城市名称。

After that, there has to be the zip code, which is the only part that is absolutely necessary because it's the only constant part. Everything after the zipcode is considered as the city name.

这篇关于正则表达式将德国地址拆分为其部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆