0

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

3
  • How can you be sure that you will match a letter belonging to the number, but not to the street name? See this demo. Are there any assumptions? Commented Oct 2, 2015 at 12:47
  • If only one letter follow the number it means that this is an information related to the number my datas are build on this principle . I have tested your regexp and works perfectly . (You can ad the answer for other people) and thank you . Commented Oct 2, 2015 at 12:52
  • Edit : what if i don't search only one letter but a specific word ? The example should be : 25 BIS Down Street ? the specific word here is BIS Commented Oct 2, 2015 at 12:57

1 Answer 1

1

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/' 
                             ^
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.