1

I was looking for a valid UK postcode regex pattern, but seems that any of them work as a traditional or normal regex.

I test all patterns using this tool: https://regex101.com/

The regex I found in this document https://www.gov.uk/government/uploads/system/uploads/attachment_data/file/413338/Bulk_Data_Transfer_-_additional_validation_valid_from_March_2015.pdf is not working.

I tried all regex in this wiki page https://en.wikipedia.org/wiki/Talk:Postcodes_in_the_United_Kingdom but nothing.

Am I doing something wrong?

I'm writing an Angular directive.

var regex = ????;

// add a parser that will process each time the value is 
// parsed into the model when the user updates it.
ngModel.$parsers.unshift(function(value) {
    // test and set the validity after update.
    var valid = regex.test(value);
    console.log(valid);
    ngModel.$setValidity('ukPostcode', valid);

    // if it's valid, return the value to the model, 
    // otherwise return undefined.
    return valid ? value : undefined;
});

Thank you.

Pablo Ezequiel Leone
  • 1,337
  • 17
  • 22
  • 2
    In what way are they not working? Not matching valid postcodes? Letting invalid ones through? Both? Something else? – James Thorpe Aug 07 '15 at 10:38
  • Validation is failing for the following postcodes: rg224ph, rg141de. I lived there, so they are 100% valid postcodes. – Pablo Ezequiel Leone Aug 07 '15 at 11:22
  • In spite of your statement: "I test all patterns using this tool: https://regex101.com/" `[A-PR-UWYZa-pr-uwyz0-9][A-HK-Ya-hk-y0-9][AEHMNPRTVXYaehmnprtvxy0-9]?[ABEHMNPRVWXYabehmnprvwxy0-9]?{1,2}[0-9][ABD-HJLN-UW-Zabd-hjln-uw-z]{2}|(GIRgir){3} 0(Aa){2})$` does not compile there: https://regex101.com/r/dM9eC1/1 – Jonathan Mee Aug 07 '15 at 11:33
  • 1
    Try [`^(?:[A-PR-UWYZa-pr-uwyz0-9][A-HK-Ya-hk-y0-9][AEHMNPRTVXYaehmnprtvxy0-9]?[ABEHMNPRVWXYabehmnprvwxy0-9]{1,2}[0-9][ABD-HJLN-UW-Zabd-hjln-uw-z]{2}|(GIRgir){3} 0(Aa){2})$`](https://regex101.com/r/kC4rN0/1). It is true that you have 2 issues in your regex: the group is not set correctly as the leading `(` is not present, and the `{1,2}` limiting quantifier is set after `?` which is not allowed. Actually, I do not understand what you want to match with [`(GIRgir){3} 0(Aa){2}`](https://regex101.com/r/kC4rN0/2): it matches `GIRgirGIRgirGIRgir 0AaAa`. – Wiktor Stribiżew Aug 07 '15 at 11:34
  • Any of this regexp were written by me, that's why I stated the URLs where I've got them from. So the official RegExp are wrong!!! – Pablo Ezequiel Leone Aug 11 '15 at 08:48

1 Answers1

0

When your regex is corrected it matches both:

rg224ph
rg141de

Your regex currently has two errors:

[A-PR-UWYZa-pr-uwyz0-9][A-HK-Ya-hk-y0-9][AEHMNPRTVXYaehmnprtvxy0-9]?[ABEHMNPRVWXYabehmnprvwxy0-9]?{1,2}[0-9][ABD-HJLN-UW-Zabd-hjln-uw-z]{2}|(GIRgir){3} 0(Aa){2})
                                                                                                 ^ {1,2} Preceding token is not quantifiable

and

[A-PR-UWYZa-pr-uwyz0-9][A-HK-Ya-hk-y0-9][AEHMNPRTVXYaehmnprtvxy0-9]?[ABEHMNPRVWXYabehmnprvwxy0-9]?{1,2}[0-9][ABD-HJLN-UW-Zabd-hjln-uw-z]{2}|(GIRgir){3} 0(Aa){2})
                                                                                                                                                                ^ ) Unbalanced parenthesis

I presume that the regex you intended was:

([A-PR-UWYZa-pr-uwyz0-9][A-HK-Ya-hk-y0-9][AEHMNPRTVXYaehmnprtvxy0-9]?[ABEHMNPRVWXYabehmnprvwxy0-9]{1,2}[0-9][ABD-HJLN-UW-Zabd-hjln-uw-z]{2}|(GIRgir){3} 0(Aa){2})

Which matches both inputs.

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288