有效的英国邮政编码正则表达式模式?

时间:2014-01-06 21:56:58

标签: regex

英国邮政编码给出了以下模式:

([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)

有人可以为我打破这个吗?

3 个答案:

答案 0 :(得分:3)

Here它是:

NODE                     EXPLANATION
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [A-PR-UWYZ0-9]           any character of: 'A' to 'P', 'R' to
                             'U', 'W', 'Y', 'Z', '0' to '9'
----------------------------------------------------------------------
    [A-HK-Y0-9]              any character of: 'A' to 'H', 'K' to
                             'Y', '0' to '9'
----------------------------------------------------------------------
    [AEHMNPRTVXY0-9]?        any character of: 'A', 'E', 'H', 'M',
                             'N', 'P', 'R', 'T', 'V', 'X', 'Y', '0'
                             to '9' (optional (matching the most
                             amount possible))
----------------------------------------------------------------------
    [ABEHMNPRVWXY0-9]?       any character of: 'A', 'B', 'E', 'H',
                             'M', 'N', 'P', 'R', 'V', 'W', 'X', 'Y',
                             '0' to '9' (optional (matching the most
                             amount possible))
----------------------------------------------------------------------
     {1,2}                   ' ' (between 1 and 2 times (matching the
                             most amount possible))
----------------------------------------------------------------------
    [0-9]                    any character of: '0' to '9'
----------------------------------------------------------------------
    [ABD-HJLN-UW-Z]{2}       any character of: 'A', 'B', 'D' to 'H',
                             'J', 'L', 'N' to 'U', 'W' to 'Z' (2
                             times)
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    GIR 0AA                  'GIR 0AA'
----------------------------------------------------------------------
  )                        end of \1

答案 1 :(得分:2)

debuggex.com是一个非常有用的资源,用于调试正则表达式:

Debuggex Demo

答案 2 :(得分:1)

我实际上更喜欢其他两个答案中的任何一个,而不是我要列出的答案,但是越多越好:

http://regex101.com/ - 会给出一个很好的细分/解释

http://www.regexper.com/ - 将产生一个可爱的铁路图:

enter image description here

以下答案也值得阅读,以获得轻微的替代/解释:

UK Postcode Regex (Comprehensive)

UK Postcode Regex