3

This regular expression for validating postcodes works perfect

^([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])))) {0,1}[0-9][A-Za-z]{2})$

but I want to split the postcodes to retrieve the individual parts of the postcode using java.

How can this be done in java?

crowne
  • 8,456
  • 3
  • 35
  • 50
Farouk Alhassan
  • 3,780
  • 9
  • 51
  • 74
  • 1
    Reference: http://en.wikipedia.org/wiki/UK_Postcodes#Format ... this is going to be a hell of a regex plus the rules may change. What do you need this for? – Pekka Apr 10 '11 at 10:45
  • I need it for search box where a user can enter a postcode. I'm mainly interested in the first part so entering only the first part or a full valid postcode satisfies the use case. – Farouk Alhassan Apr 10 '11 at 11:04

3 Answers3

5

Here are the official regexes for matching UK postcodes:

http://interim.cabinetoffice.gov.uk/media/291370/bs7666-v2-0-xsd-PostCodeType.htm

If you want to split a found postcode into it's two parts, isn't it simply a question of splitting on whitespace? A UK postcode's two parts are just separated by a space, right? In java this would be:

String[] fields = postcode.split("\\s");

where postcode is a validated postcode and fields[] will be an array of length 2 containing the first and second parts.

Edit: If this is to validate user input, and you want to validate the first part, your regex would be:

Pattern firstPart = Pattern.compile("[A-Z]{1,2}[0-9R][0-9A-Z]?");

To validate the second part it is:

Pattern secondPart = Pattern.compile("[0-9][A-Z-[CIKMOV]]{2}");
Richard H
  • 38,037
  • 37
  • 111
  • 138
  • I think he's asking about the case when it's not separated by a space. The rules to do it in that case seem fairly complicated. – Pekka Apr 10 '11 at 10:52
  • @Pekka - a postcode with no space will fail validation. Perhaps I misunderstand, but I though the OP wanted to split validated addresses? – Richard H Apr 10 '11 at 10:53
  • @Richard yeah, the OP needs to clarify. Check out the Wikipedia link above, maybe he needs *all* separate components. – Pekka Apr 10 '11 at 10:55
  • I just need the two main parts but want to cover for cases where spaces have not being entered which is handled by the above regex for validation – Farouk Alhassan Apr 10 '11 at 10:56
  • 1
    In that case, without looking at this more closely for a more elegant approach, you may have to check 7 different regex matches for each postcode. – Richard H Apr 10 '11 at 11:02
  • @Farouk: i have added the two regexes for matching the first and second parts of a postcode – Richard H Apr 10 '11 at 11:10
  • 1
    As second parts seems to be always `9AA`, OP should to remove spaces and last three characters. – Rubens Farias Apr 10 '11 at 11:13
  • @Richard H The URL in this answer is broken. I think this is the same document. http://developer.k-int.com/svn/default/ispp/docs/schemas/bs7666-v2-0.xsd – Mark Chorley Apr 18 '13 at 20:48
1

I realise that it's rather a long time since this question was asked, but I had the same requirement and thought that I'd post my solution in case it helps someone out there :

const string matchString = @"^(?<Primary>([A-Z]{1,2}[0-9]{1,2}[A-Z]?))(?<Secondary>([0-9]{1}[A-Z]{2}))$";

var regEx = new Regex(matchString);
var match = regEx.Match(Postcode);

var postcodePrimary = match.Groups["Primary"];
var postcodeSecondary = match.Groups["Secondary"];

This doesn't validate the postcode, but it does split it into 2 parts if no space has been entered between them.

Stephan
  • 41,764
  • 65
  • 238
  • 329
Gareth
  • 33
  • 1
  • 6
  • You can stick an optional whitespace in between the two groups, then it will match with or without a space and split into two parts. I am unsure how to do that in this flavour of regex but normally something like /s? – Mark Chorley Apr 18 '13 at 20:49
-1

You can use the Google's recentlly open sourced library for this. http://code.google.com/p/libphonenumber/

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327