module UK Postcode = begin # The letters Q, V and X are not used in the first position... alpha_a = '[ABCDEFGHIJKLMNOPRTSUWYZ]' # The letters I, J and Z are not used in the second position... alpha_b = '[ABCDEFGHKLMNOPQRSTUVWXY]' alpha_c = '[ABCDEFGHJKSTUW]' alpha_d = '[ABEHMNPRVWXY]' part1_a = %{#{alpha_a}\\d{1,2}} part1_b = %{#{alpha_a}#{alpha_b}\\d{1,2}} part1_c = %{#{alpha_a}\\d#{alpha_c}} part1_d = %{#{alpha_a}#{alpha_b}\\d#{alpha_d}} part1 = %{(#{part1_a}|#{part1_b}|#{part1_c}|#{part1_d})} # the letters C, I, K, M, O and V are not used in the 2nd part... part2 = %{\\d[ABDEFGHJLNPQRSTUWXYZ]{2}} # GIR 0AA is historic and in use, but invalid according to modern rules... pattern = /(GIR 0AA|#{part1} #{part2})/i end end