HopefulRiver
HopefulRiver

Reputation: 1

Exclude specific phrase from existing regular expression

This regular expression detects any UK post codes:

([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9][A-Za-z]?))))\s?[0-9][A-Za-z]{2})

I would like to blacklist/exclude a certain post codes for example 'GU14 6XM'

Flavor: PCRE (PHP <7.3) Regex flags: global, multi line, insensitive & single line

I have tried to combine the following into the existing Regex but I cannot get it to work, they work individually but not together.

^(?!.GU14 6XM).$

Also tried using

(?=) - positive lookahead
(?!) - negative lookahead
(?<=) - positive lookbehind
(?<!) - negative lookbehind

([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9][A-Za-z]?))))\s?[0-9][A-Za-z]{2})(?<!GU14 6XM).*

I would like the post code GU14 6XM to be ignored but should GU14 6XP or any other combination of UK post code appear I need that to trigger.

Upvotes: 0

Views: 76

Answers (0)

Related Questions