Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

297
Visualizações
Regex optional groups and digit length

Maybe some regex-Master can solve my problem.

I have a big list with many addresses with no seperators( , ; ). The address string contains following Information:

  • The first group is the street name
  • The second group is the street number
  • The third group is the zipcode (optional)
  • The last group is the town name (optional)

regex_png

As you can see on the image above the last two test strings are not matching. I need the last two regex groups to be optional and the third group should be either 4 or 5 digits.

I tried (\d{4,5}) for allowing 4 and 5 digits. But this only works halfways as you can see here: https://regex101.com/r/ZurqHh/1
regex_4_5_digits (This sometimes mixes the street number and zipcode together)

I also tried (?:\d{5})? to make the third and fourth group optional. But this destroys my whole group layout... https://regex101.com/r/EgxeMy/1

regex_optional

This is my current regex:

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

Try it out yourself: https://regex101.com/r/zC8NCP/1

My brain is only farting at this moment and i can't think straight anymore.

Please help me fix this problem so i can die in peace.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can use

^(.*?)(?:\s+(\d+(?:\s*[-|+\/]\s*\d+)*\s*[a-z]?\b))?(?:\s+(\d{4,5})(?:\s+(.*))?)?$

See the regex demo (note all \s are replaced with \h to only match horizontal whitespaces).

Details:

  • ^ - start of string
  • (.*?) - Group 1: any zero or more chars other than line break chars
  • (?:\s+(\d+(?:\s*[-|+\/]\s*\d+)*\s*[a-z]?\b))? - an optional non-capturing group matching
    • \s+ - one or more whitespaces
    • (\d+(?:\s*[-|+\/]\s*\d+)*\s*[a-z]?\b) - Group 2:
      • \d+ - one or more digits
      • (?:\s*[-|+\/]\s*\d+)* - zero or more sequences of zero or more whitespaces, -, +, | or /, zero or more whitespaces, one or more digits
      • \s* - zero or more whitespaces
      • [a-z]?\b - an optional lowercase ASCII letter and a word boundary
  • (?:\s+(\d{4,5})\b(?:\s+(.*))?)? - an optional non-capturing group matching
    • \s+ - one or more whitespaces
    • (\d{4,5}) - Group 3: four or five digits
    • (?:\s+(.*))? - an optional sequence of one or more whitespaces and then any zero or more chars other than line break chars as many as possible
  • $ - end of string.

Please note that the (?:\s+(.*))? optional group must be inside the (?:\s+(\d{4,5})...)? group to work.

over 4 years ago · Santiago Trujillo Relatório

0

It is difficult to parse addresses because we are halfway between formatted text and natural language. Here is a pattern that tries as much as possible to reduce the number of optional parameters to succeed with the examples offered without asking too much to the regex engine. To do this, I mainly rely on character classes, atomic groups, and a relatively accurate description of the street names. Obviously, all the examples of the question cannot be representative of reality and characters could be added or removed from the classes to deal with new cases. Nevertheless, the structure of this pattern is a good starting point.

~
^
(?<strasse> [\pL\d-]+ \.? (?> \h+ [\pL\d-]+ \.? )*? ) \h*
(?<nummer> \b (?> \d+ | [-+/\h]+ | [a-z] \b )*? )
(?: \h+ (?<plz> \d{4,5} )
    \h+ (?<stadt> .+ ) )?
$
~mxui

demo

Note that in the above link you can also see a previous version of this pattern with a more accurate description of the street number (a bit more efficient but longer).

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda