Skip to main content
Solved

Splitting an address

  • January 29, 2017
  • 2 replies
  • 102 views

gisgeek
Contributor
Forum|alt.badge.img+9

Hi I am trying to use a regular expression to split an address. It sounds relatively simple but I keep getting caught out on some of the address inputs:

Here are the scenarios. So I want to split the address after the - but in these scenarios I might have a range like the second one or there might be a space after the name or there might not.

On Gas Limited- 37 Hunua Road

Ixom Operations Pty Limited - 135-147 Waterloo Road

Best answer by jeroenstiers

Hi @gisgeek

Great you figured out the answer! However I would like to suggest to improvements depending on the result you want. (1) I am assuming that you don't want the hyphen to be part of your address and (2) that in some occasions, the first part of the string might contain a hyphen to.

1/ You can make use of a so called 'positive lookbehind' to select everything following a specific regex. In this case you could select everything which is following a hyphen. Using this method, you will remove the hyphen and leading spaces from the result.

 

Example:

 

input: On Gas Limited- 37 Hunua Road

 

regex: (?<=-).*

result: ' 37 Hunua Road'

2/ You might want to make the regex a bit more strict to allow hyphens in the first part of the string. If you are certain that the address always starts with a numeric value, use this information

input: On Gas - Limited - 115 - 116 Hunua Road

 

regex: (?<=-)\\s*\\d+.*

result: ' 115 - 116 Hunua Road'

Try the regex here

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

2 replies

gisgeek
Contributor
Forum|alt.badge.img+9
  • Author
  • Contributor
  • January 29, 2017

Oh yaye I think I worked it out (-)\\s seems to work


Forum|alt.badge.img+7
  • Best Answer
  • January 30, 2017

Hi @gisgeek

Great you figured out the answer! However I would like to suggest to improvements depending on the result you want. (1) I am assuming that you don't want the hyphen to be part of your address and (2) that in some occasions, the first part of the string might contain a hyphen to.

1/ You can make use of a so called 'positive lookbehind' to select everything following a specific regex. In this case you could select everything which is following a hyphen. Using this method, you will remove the hyphen and leading spaces from the result.

 

Example:

 

input: On Gas Limited- 37 Hunua Road

 

regex: (?<=-).*

result: ' 37 Hunua Road'

2/ You might want to make the regex a bit more strict to allow hyphens in the first part of the string. If you are certain that the address always starts with a numeric value, use this information

input: On Gas - Limited - 115 - 116 Hunua Road

 

regex: (?<=-)\\s*\\d+.*

result: ' 115 - 116 Hunua Road'

Try the regex here