From the code you sent me, you had a syntax error. The parenthesis at ([aA][lL]
was extraneous, so the regex stopped working.
It overall looks good but there’s something to fix. Remember this: that any postcode with an extra number that starts with one of the above
?
I am assuming that will not apply for WD23
(so you don’t want to match WD236) but since the \d? applies to any of the previously matched group, that happens.
So, this will work if any of the single digit groups you’ve done (AL6, WD4, HP3, etc.) might have an extra number after them and you want to accept them, but the double digit groups (AL10, WD25, etc) do not.
^([sS][gG]1|[aA][lL][6-7|10|2-3]|[wW][dD][23|25|7|4]|[hH][pP][1|3-4]|[lL][uU]1)(?<!\d{2})\d?\b
What I added was (?<!\d{2})
before \d?
, making it this expression: (?<!\d{2})\d?
This means it’s gonna do a look behind (?<
) and it’s a negative affirmation (!
) of two digits (\d{2}
), and it means it’ll apply the expression after the parenthesis that contains the look behind - \d?
is after (?<!\d{2})
- to strings that DON’T already have two digits.
AL67 passes (7 was allowed by the \d after the negative lookbehind, as 6 is only one digit. But WD236 does not, as WD23 already has two digits.
Ok, now that’s with the assumption any single digit group you wrote out would be able to accept any other digit after it. But if it doesn’t, like AL4 could accept it but WD7 or HP3 cannot, I think you’ll need to a more complicated lookbehind, or put the \d?
into the different OR (|
) groups rather than apply to all of them. If that’s the case say so, then I can help, but for now that’s my solution.