I have a string with space separated addresses and I want to separate the number from the street name.
So if we have :
Street Blah Blah 34
or
34 Street Blah Blah
I want a regex to match the "Street Blah Blah" and another to match "34"
It can get more complex with addresses like this:
Überbrückerstraße 24a.
where it should return "24a" and the rest as a street or
Järnvägstationg. 3/B
where it should return 3/B and the rest as a street etc.
I am currently doing this using C# where I split all strings by space and return whichever string contains at least one number and then return all the rest as a street.
However I was wondering if it would be more elegant and more efficient to do this with Regex.
I've been fiddling with regex but I couldn't find a robust way so far. Any ideas?
Here are some unit test data. Input street, Expected premise number and expected street:
[TestCase("Järvägstationg. 3/B", "3/B", "Järvägstationg.")]
[TestCase("Überbrückerstraße 24a", "24a", "Überbrückerstraße")]
[TestCase("Street Blah Blah 34", "34", "Street Blah Blah")]
[TestCase("34 Street Blah Blah", "34", "Street Blah Blah")]
[TestCase("Ueckerstr. 20 b", "20 b", "Ueckerstr.")]
[TestCase("Elmshornerstraße 163", "163", "Elmshornerstraße")]
[TestCase("Hallgartenerstrasse Moritzstr.", "", "Hallgartenerstrasse Moritzstr.")]
[TestCase("19 Green Lane", "19", "Green Lane")]
I think out of these the
Ueckerstr. 20 b
is the trickiest, in which case, I don't mind if that one fails for now.