I am looking to match a postcode using the following regex:
(GIR ?0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]([0-9ABEHMNPRV-Y])?)|[0-9][A-HJKPS-UW]) ?[0-9][ABD-HJLNP-UW-Z]{2})
I am trying to parse an address from an HTML document and as such I only want to match nodes that start with a postcode or contain a postcode that is preceded by a space or comma. Otherwise there are too many false positives e.g. matching colours (preceded by #).
I need to amend the regex to either find the postcode with no preceding characters or a space or comma immediately preceding it and any number of characters before this. How can I do this?
For example, I would want to match:
IP14 2PL
1 The street, ipswich, IP14 2PL
1 The street, ipswich,IP14 2PL
BUT NOT
https://t.co/ip142plzruc