MaDo
MaDo

Reputation: 21

German address - regular expression

I have a webcall that give me the following callback as string:

ZIPCODE | CITY | STREET NR | , |Phone Number with (Tel.) on the beginning

EXAMPLE1 :

83661 Lenggries Marktstr. 14, Tel. 08042 91860

I would like to ask the regex professionals if it would be possible to break them down into groups? My best try at the moment is:

([0-9]{5})(\s[a-zA-Z].+)(\s[a-zA-Z].+)(\s.*)(\s.[0-9]*)(\s.[0-9]*)

which results in:

  1. 83661
  2. Lenggries
  3. Marktstr. 14,
  4. Tel.
  5. 08042
  6. 91860

This works for the sample above but is not a stable regex, for example:

EXAMPLE2:

83620 Feldkirchen-Westerham Aiblinger Str. 7, Tel. 08063 8562

results in:

  1. 83620
  2. Feldkirchen-Westerham Aiblinger
  3. Str. 7,
  4. Tel.
  5. 08063
  6. 8562

Maybe somebody can help me with this problem, I have no idea how to solve it. The data cannot be given in any other format either. :(

regards and thanks in advance Matthias

Upvotes: 2

Views: 470

Answers (2)

M. Atif Altaf
M. Atif Altaf

Reputation: 1

Check it in Python!

^[ \-0-9a-zA-ZäöüÄÖÜß.]+?\s+(\d+(\s?[a-zA-Z])?)\s*(?:$|\(|[A-Z]{2})\s+(\d{5})\s*(.+)

if you want to see the demo! Click here

Upvotes: 0

Alireza
Alireza

Reputation: 2123

Try this:

^(\d{5})\s*([a-zA-Z].+)(?=\b\d+,)(\d+).*Tel.\s([\d ]+)$

See Demo in PCRE

See C# Code Demo

Upvotes: 1

Related Questions