RegEx for matching UK Postcodes

前端 未结 30 2961
广开言路
广开言路 2020-11-22 01:38

I\'m after a regex that will validate a full complex UK postcode only within an input string. All of the uncommon postcode forms must be covered as well as the usual. For in

30条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 01:45

    According to this Wikipedia table

    enter image description here

    This pattern cover all the cases

    (?:[A-Za-z]\d ?\d[A-Za-z]{2})|(?:[A-Za-z][A-Za-z\d]\d ?\d[A-Za-z]{2})|(?:[A-Za-z]{2}\d{2} ?\d[A-Za-z]{2})|(?:[A-Za-z]\d[A-Za-z] ?\d[A-Za-z]{2})|(?:[A-Za-z]{2}\d[A-Za-z] ?\d[A-Za-z]{2})
    

    When using it on Android\Java use \\d

提交回复
热议问题