Reputation: 541
i know that regex on postal address cannot be optimal, but i really one here .
I explain my problem : I got different types of postal address :
32 Rue Jean Jaures 69000 Lyon
Bâtiment 1 32 Rue Jean Jaures 69000 Lyon
32 B Jean Jaures 69000 Lyon
Bâtiment 1 32 B Rue Jean Jaures 69000 Lyon
I need a regexp to find only the number of the street in any position.
I have done a regexp which permit to determine the number if it is at the beginning of the string :
`^([1-9][0-9]{0,2}(?:\s*[A-Z])?)\b`
You can see the result here : https://regex101.com/r/dY7cE6/3
But the problem is that i can't find it if it's not the first number of my string (like this address : Bâtiment 1 32 Rue Jean Jaures 69000 Lyon)
So i ask you help to find in any situation this street's number wich is here "32".
I keep search on my own but help will be appreciate .
Thank you .
Upvotes: 2
Views: 210
Reputation: 6511
Last number in the string, unless it's 5 digits, optionally capturing a single letter after the number:
^.*\b(?!\d{5}[A-Z]?\b)(\d+(?:\s*[A-Z]\b)?)
Upvotes: 1
Reputation: 67968
\b(?!\d{5}\b)\d+\b(?:\s*\w\b)?(?=\D*\b\d{5}\b|\D*$)
Try this.See demo.
https://regex101.com/r/cJ6zQ3/20
Upvotes: 1
Reputation:
The following regex will find the second to last number in the string (in the capture group). If there is a single letter after this number (separated by a space or not) it will also capture this.
It requires the last number in the string to be a five digit postcode:
/(\d+(?:\s*\w\b)?)[^\d]+\d{5}[^\d]+$/
However, how reliably do you need to identify house number? What is the possible range of input data? No regex approach is likely to be very good. This question and answers give some idea of the problems.
See it working on the sample data.
Upvotes: 1