0

I have UK postcodes stored in a database like this:

EC1-3

S40-45

NE99

So it's the first part of a postcode, followed by a range of numbers (or just one number if there's not a range). So for example, I need a regex that will match EC1-3 with an input of EC1V, but not with EC4R.

Is this possible?

Tom
  • 1,561
  • 4
  • 20
  • 29
  • 2
    Can you provide more examples of what you expect it to match? – Simon Whitehead Nov 20 '13 at 09:53
  • @devnull that's what is was thinking about. EC1-3 is the 1-3 part literal or a range of 1,2 or 3? be more specific please. Also, more test strings please... – Cas Nouwens Nov 20 '13 at 09:54
  • 1
    How about this?
    http://stackoverflow.com/questions/164979/uk-postcode-regex-comprehensive
    – ASA Nov 20 '13 at 09:55
  • Well now I feel silly. :P It's as simple as that? I think that ought to do it. I'll try it out and see. Thanks. – Tom Nov 20 '13 at 09:56
  • @Traubenfuchs, nice find man. – Cas Nouwens Nov 20 '13 at 09:57
  • Thanks Traubenfuchs, but that seems to be for matching a full postcode, whereas I just need to match the first part, which isn't quite so complex. devnull's solution seems to do the trick. – Tom Nov 20 '13 at 10:01
  • @Tom I have a C# struct on GitHub that will allow you to match the first or whole of postcodes, complete with unit tests - https://github.com/ianfnelson/Postcode – Ian Nelson Nov 20 '13 at 10:21
  • Thanks Ian. That's a bit more than I need though. I'll keep it in mind if I do need it one day though! – Tom Nov 20 '13 at 15:44

0 Answers0