1

I have the following regular expression for matching UK postcodes:

GIR[ ]?0AA|((AB|AL|B|BA|BB|BD|BH|BL|BN|BR|BS|BT|CA|CB|CF|CH|CM|CO|CR|CT|CV|CW|DA|DD|DE|DG|DH|DL|DN|DT|DY|E|EC|EH|EN|EX|FK|FY|G|GL|GY|GU|HA|HD|HG|HP|HR|HS|HU|HX|IG|IM|IP|IV|JE|KA|KT|KW|KY|L|LA|LD|LE|LL|LN|LS|LU|M|ME|MK|ML|N|NE|NG|NN|NP|NR|NW|OL|OX|PA|PE|PH|PL|PO|PR|RG|RH|RM|S|SA|SE|SG|SK|SL|SM|SN|SO|SP|SR|SS|ST|SW|SY|TA|TD|TF|TN|TQ|TR|TS|TW|UB|W|WA|WC|WD|WF|WN|WR|WS|WV|YO|ZE)(\d[\dA-Z]?[ ]?\d[ABD-HJLN-UW-Z]{2}))|BFPO[ ]?\d{1,4}/gi

From what I understand I can add /gi to the end to make it match case insensitive however this doesn't seem to work at all? Can anyone see why?

dtsg
  • 4,408
  • 4
  • 30
  • 40
  • 3
    What is your regex engine? Depending on which one you're using, using `/gi` might not work and instead `(?i)` at the start of the expression will make the expression case insensitive. – Jerry Jul 03 '13 at 15:31
  • @Jerry .NET, this regex is fed to a `RegularExpressionValidator` control on an Asp.net page – dtsg Jul 03 '13 at 15:32
  • You shouldn't need to add `g` - only `i` - but when you say "doesn't seem to work", what do you mean? – Aleks G Jul 03 '13 at 15:33
  • @AleksG `when you say "doesn't seem to work", what do you mean?` I mean it doesn't match UK postcodes when entered lower case. Uppercase is fine – dtsg Jul 03 '13 at 15:33
  • possible duplicate of [Make regular expression case insensitive in ASP.NET RegularExpressionValidator](http://stackoverflow.com/questions/2641236/make-regular-expression-case-insensitive-in-asp-net-regularexpressionvalidator) – cb0 Jul 03 '13 at 15:34
  • See [this answer](http://stackoverflow.com/a/2641246/1578604). – Jerry Jul 03 '13 at 15:34
  • @Jerry Initial testing with `(?i)` at the start of the expression seems to work flawlessly. I would add this as the answer – dtsg Jul 03 '13 at 15:36
  • I know that I can use `RegexOptions.IgnoreCase` however the system I'm building reads in different regexs per country where some actually require case sensitivity which would mean I would have to code specific logic to handle UK postcodes whereas I would prefer this all to be handled by the expression itself – dtsg Jul 03 '13 at 15:42

3 Answers3

3

Just uppercase your postcode string before putting it through your regex.
Also have a look at this question on UK postcodes

Community
  • 1
  • 1
zeFrenchy
  • 6,541
  • 1
  • 27
  • 36
2

If you're using .Net perhaps use RegexOptions.IgnoreCase when you construct? Taken from here.

That page also has a link to this expression which might be worth trying.

Nick
  • 2,285
  • 2
  • 14
  • 26
2

Depending on which one you're using, using /gi might not work and instead (?i) at the start of the expression will make the expression case insensitive.

So, you'd use:

(?i)GIR[ ]?0AA|((AB|AL|B|BA|BB|BD|BH|BL|BN|BR|BS|BT|CA|CB|CF|CH|CM|CO|CR|CT|CV|CW|DA|DD|DE|DG|DH|DL|DN|DT|DY|E|EC|EH|EN|EX|FK|FY|G|GL|GY|GU|HA|HD|HG|HP|HR|HS|HU|HX|IG|IM|IP|IV|JE|KA|KT|KW|KY|L|LA|LD|LE|LL|LN|LS|LU|M|ME|MK|ML|N|NE|NG|NN|NP|NR|NW|OL|OX|PA|PE|PH|PL|PO|PR|RG|RH|RM|S|SA|SE|SG|SK|SL|SM|SN|SO|SP|SR|SS|ST|SW|SY|TA|TD|TF|TN|TQ|TR|TS|TW|UB|W|WA|WC|WD|WF|WN|WR|WS|WV|YO|ZE)(\d[\dA-Z]?[ ]?\d[ABD-HJLN-UW-Z]{2}))|BFPO[ ]?\d{1,4}
Jerry
  • 70,495
  • 13
  • 100
  • 144