United Kingdom (GB) postal code validation without regex

前端 未结 8 1821
青春惊慌失措
青春惊慌失措 2020-12-18 22:25

I have tried several regexes and still some valid postal codes sometimes get rejected.

Searching the internet, Wikipedia and SO, I could only find regex validation s

8条回答
  •  隐瞒了意图╮
    2020-12-18 23:02

    Regexes are hard to debug, hard to port from one regex flavor to another (silent "errors"), and hard to update.

    That is true for most regexes, but why don't you just split it up into multiple parts? You can easily split it into six parts for the six different general rules and maybe even more if you take all of the special cases into account.

    Creating a well-commented method of 20 lines with simple regexes is easy to debug (one simple regex per line) and also easy to update. The porting problem is the same, but on the other hand you do not need to use some fancy grammar lib.

提交回复
热议问题