I have a collection of all uppercase address names and numbers and I want to extract just the first encountered address number for each address. The following examples show what I would like to extract from each:
- 80 ROSE COTTAGE -> 80
- 80A ROSE COTTAGE -> 80A
- 80 A ROSE COTTAGE -> 80 A
- 80ROSE COTTAGE -> 80 (accidental no-space)
- [ANY OTHER TEXT] 80 ROSE COTTAGE -> 80
I have found some similar questions answered here and elsewhere on the internet, but they always deal with an address as a whole as opposed to specifically just address name and number:
Match each address from the address number to the 'street type'
Regular Expression: Any character that is NOT a letter or number
javascript regular expressions address number
JavaScript regex to validate an address
The last one makes reference to a lookahead, which lead me to construct a negative look ahead for any alphanumeric characters following a potential single text character(eg. 80 A) in my JavaScript regex. However without adding the alternative "digits only found" group (\d+) my fourth example above does not return just the number.
(?:\d+\s*[A-Z]?(?![A-Z0-9]))|(?:\d+))
Is there a way to combine these two groups into a single regex expression? Or is this not possible in JavaScript's regex implementation?
Any help with this would be greatly appreciared.