I am trying to extract a possible postcode from anywhere in string using PRXCHANGE.
PRXCHANGE ( "s/^.*([A-Z][A-Z]?[0-9][0-9A-Z]?\s?[0-9][A-Z][A-Z]).*$/$1/" , -1 , a.address )
The regular expression correctly identifies postcodes but the replacement always removes the first letter of the postcode such that AZ12 3ZA will become Z12 3ZA.
I attempted tried several variation of the first two values such as [A-Z]{1,2} but always had the same issue.
I added spaces at the start and end of the postcode regex as follows;
PRXCHANGE ( "s/^.*( [A-Z][A-Z]?[0-9][0-9A-Z]?\s?[0-9][A-Z][A-Z] ).*$/$1/" , -1 , a.address )
This seems to fix the problem and correctly returns possible postcodes. I don't understand why the space after the open bracket has worked. Can anyone explain why the first doesn't work but the second does?
Thanks