IBAN Regex to include spaces and dash

时间:2017-07-14 21:10:09

标签: regex iban

I'm trying to create a regex that would allow/register a IBAN number even if it includes dashes or spaces.

EX. Greece IBAN #

GR1601101250000000012300695

Greece IBAN Regex

GR\d{2}\d{4}\d{3}\w\w{4}\w{4}\w{4}\w{3}

I want to combine these three(3) regexes into one(1) regex:

GR\d{2} \d{4} \d{3}\w \w{4} \w{4} \w{4} \w{3} (spaces)

GR\d{2}\d{4}\d{3}\w\w{4}\w{4}\w{4}\w{3} (no spcaes, no dashes)

GR\d{2}-\d{4}-\d{3}\w-\w{4}-\w{4}-\w{4}-\w{3} (dashes)

Is this possible?

Any help that anyone can provide would be greatly appreciated. Thanks!

1 个答案:

答案 0 :(得分:1)

根据您所使用的语言,您可能会做一些更复杂的事情,但最简单的方法就是使用交替:

GR\d{2}( \d{4} \d{3}\w \w{4} \w{4} \w{4} \w{3}|\d{7}\w{16}|-\d{4}-\d{3}\w-\w{4}-\w{4}-\w{4}-\w{3})

例如,使用Perl或C#样式正则表达式,您可以记住并引用匹配项:

GR\d{2}(?<sep>[ -]?)\d{4}\k<sep>\d{3}\w\k<sep>\w{4}\k<sep>\w{4}\k<sep>\w{4}\k<sep>\w{3}