Skip to main content

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

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


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


Reply