'Using Python re and findall to match complex combination of digits in string

Im trying to use python re library in order to analyze a string containing a street name and multiple (or just a single) numbers separated by a forward slash.

example = 'Examplestreet 1/2.1/3a/10/10.1/11b/12a-12c/13a-c'

I want to match all digits, including positions after the dot and adjacent alpha characters. If a hyphen connects two numbers with an alpha character, they should also be considered as one match.


Expected output:

['1', '2.1', '3a', '10', '10.1', '11b', '12a-12c', '13a-c']

I'm trying the following

numbers = re.findall(r'\d+\.*\d*\w[-\w]*', example)

Which is able to find all except single non-float digits (i.e. '1'):

print(numbers)

['2.1', '3a', '10', '10.1', '11b', '12a-12c', '13a-c'] 

How do I need to tweak my regex in order to achieve the desired output?



Solution 1:[1]

this works:

 numbers = re.findall(r'\d[0-9a-z\-\.]*', example)

Solution 2:[2]

Using Regex

Working example : https://regex101.com/r/PDYSgH/1

import re
example = 'Examplestreet 1/2.1/3a/10/10.1/11b/12a-12c/13a-c'
numbers = re.findall(r'\d[a-z0-9.\-]*', example)

Using Split

Probably you can split the string using space and then /.

numbers = example.split(" ")[-1].split("/")

Solution 3:[3]

Another solution, which seems simpler:

>> re.findall(r'\d[^/]*', example)
['1', '2.1', '3a', '10', '10.1', '11b', '12a-12c', '13a-c']

You can confirm that it works here (although, I had to escape the slash (/) character).

  • \d[^/]*: Matches any string that starts with a digit and is followed up by any character, except a / (stops at said character).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Rizal Ardhi Rahmadani
Solution 3