HI,
I'm trying to match UK postcodes, using the pattern from http://interim.cabinetoffice.gov.uk/media/291370/bs7666-v2-0-xsd-PostCodeType.htm,
/^[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z-[CIKMOV]]{2}$/
I'm using this in PHP, but it doesn't match the valid postcode OL13 0EF
. This postcode does match, however, when I remove the -[CIKMOV]
character class subtraction.
I get the impression that I'm doing character class subtraction wrong in PHP. I'd be most grateful if anyone could correct my error.
Thanks in advance for your help.
Ross
Most of the regex flavours do not support character class subtraction. Instead you could use look-ahead assertion:
/^[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9](?!.?[CIKMOV])[A-Z]{2}$/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With