Reputation: 541
I work on a project which consist to sort address .
I got a typical address for example : 25 Down Street 15000 London
.
On another side i got a more specific address with for example 25 **B** Down Street 15000 London
.
I found a way to only select number of streets with this regex : \b([1-9][0-9]{0,2})
.
But as you can see some address contains another letter which is part of the number (like 25 A , 25 B ..)
So now i search regex able to find numbers of street even if it contains more than just the number .
Example : If the address is 25 Down Street
(I want to find 25) and if the address is 25 B Down Street
(I want to find 25 B
)
Keep searching, but maybe you got an idea to help me .
Thank you
Upvotes: 0
Views: 33
Reputation: 626738
If you have only one Latin letter after the first 1-3-digit number in string, you can use the following regex:
'/^[1-9][0-9]{0,2}(?:\s*[A-Z])?\b/'
See demo
The word boundary \b
will make sure there is no word character after the Latin letter after the first number followed with optional whitespace. If the letter is missing, the \b
will still make sure the number is followed by a non-word character, which seems to fit your requirements.
If you want to just assume the next ALLCAPS word after the first number belongs to the number, use:
'/^[1-9][0-9]{0,2}(?:\s*[A-Z]+)?\b/'
^
Upvotes: 1