I have a task where I need to extract the house number from an address. The reason for extracting the next character as well is because the number may be something like 1a Example Road, 1b Example Road etc. I need to make sure I get the correct address every time.
I found a solution on the forums where I can use regex to extract any and all numbers from a string which works great, but it will not pick up the character after the numbers (if one exists).
Example Inputs:
234 Test Road, Testville
456b Tester Road, Testville
Required Outputs:
234
456b
I am open to any and all suggestions on how to do this, thanks in advance.
Thank you, this works well when the house number is at the start of the string. I also have the case where there may be a house name at the start of the string e.g. ‘House Name, 1a Test Road’ - I need only 1a here. How can I do this?
Thank you for this suggestion, I had never used the ‘Matches’ activity before but have managed to customise it so that I can return the correct address for quite a few scenarios.
The pattern I have ended up with is: “(\d+)(\w*)” so this looks for at least one digit and then following that digit, Any (0 or more) word characters.
The only issue I can see now is if the input is something like ‘1 a’ but I think I will just raise this as a business exception. Thanks again for your help.
I have never really used regex before, although I understand what it is. If you have time could you please explain exactly how your solution works? I know that \d searches for digits and \w for characters but that’s about it!
https://regex101.com/ gives you all the reference (bottom right) and allows you to test your expressions on your text.
Even if an expression is built for you by someone else, you paste it in here and this site explains it to you in a clear and concise manner (top right)