正则表达式:匹配帐号,但不匹配代币/生日

时间:2017-05-03 18:15:21

标签: javascript regex

我正在努力学习以下正则表达式:

\b[\dBb][-. \dEe]+(\d{4})\b

它应匹配帐号,但不能与令牌/生日或带有" be"的文本匹配。在里面。 在community的支持下,我已经设法捕获第一部分。但是,我似乎无法排除令牌/生日。

应该匹配的案例(我的正则表达式正常):

1234 1234 1234 1234
1234 1234 1234 1234 1
BE12 1234 1234 1234
1234-1234-1234-1234
1234.1234.1234.1234
1234123412341234
12341234 1234 1234
1234-1234-1234-1234-1
1234.1234.1234.1234.1
12341234123412341
12341 234 1234 12341
BE12-1234-1234-1234
be12-1234-1234 1234
Be12.1234.1234-1234
BE12123412341234

不应匹配的案例(我的正则表达式对其中一些不正确):

123456
Token: 123456
I shall be awesome in Belgium, as my birthdate is 01/01/2000.
01.01.2000
01012000
first of january 2000

你能帮忙解决这个挑战吗? 谢谢!

1 个答案:

答案 0 :(得分:1)

匹配BE或2位数字,后跟14或15位数字,其中散布着空格,句点和短划线

([Bb][Ee]|\d\d)([-. ]?\d){14,15}

<强>测试

ACCOUNT             INPUT
1234123412341234    1234 1234 1234 1234
12341234123412341   1234 1234 1234 1234 1
BE12123412341234    BE12 1234 1234 1234
1234123412341234    1234-1234-1234-1234
1234123412341234    1234.1234.1234.1234
1234123412341234    1234123412341234
1234123412341234    12341234 1234 1234
12341234123412341   1234-1234-1234-1234-1
12341234123412341   1234.1234.1234.1234.1
12341234123412341   12341234123412341
12341234123412341   12341 234 1234 12341
BE12123412341234    BE12-1234-1234-1234
be12123412341234    be12-1234-1234 1234
Be12123412341234    Be12.1234.1234-1234
BE12123412341234    BE12123412341234
No Match            123456
No Match            Token: 123456
No Match            I shall be awesome in Belgium, as my birthdate is 01/01/2000.
No Match            01.01.2000
No Match            01012000
No Match            first of january 2000
Be12123412341234    Here is my account number Be12.1234.1234-1234. have a nice day

验证者(Ruby):

a = [ "1234 1234 1234 1234",
...
]

puts 'ACCOUNT             INPUT'
a.each { |s|
  match = /(([Bb][Ee]|\d\d)([-. ]?\d){14,15})/ =~ s
  puts "%-18s  %s"%[ match ? $1.tr('-. ', '') : "No Match", s ]
}