Re: Validating UK postcodes
Well, it's a start.
Originally Posted by Mehwish-S
I perceive that you have already recognized that you need a regular expression.
I would make three Strings since it's easier to test one thing at a time.
I would probably call the Strings "firstPart," "separator," and "isecondPart."
Then the regular expression String used in the String.matches() statement in the final program would simply be firstPart + separator + isecondPart.
OK, let's get started...
I would (probably) do the easy part first:
The "separator" is a single space, right? (Wow! We are one third of the way through already!)
Then, I would try to express the other parts in Human Language in a form that can be translated to Regular Expression lingo as spelled out in the Java Regular Expression Tutorial
From your description, I think the Strings would be defined something like the following:
Space: [ ]
In the tutorial, here's where it tells you how you handle repeated fields: Regular Expression Quantifiers
Now if my description turns out not to be completely accurate in terms of accepting valid codes and/or rejecting invalid codes (I'm a Yank, you know; English is just another Foreign Language to me), then you might be able to use my thoughts as a starting point and refine things if your testing turns up problems.
Test, test, and test some more. Give lots of valid codes, but don't just give it Good Stuff; try to find Bad Stuff that is "almost-but-not-quite" Good.