Skip to main content
Solved

Splitting an address


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

View original
Did this help you find an answer to your question?

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


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings