我有以下有效字符串...
" 1"
" 12"
" 123"
"1234"
" 123"
" 12A"
""
以下字符串无效...
" 1234"
" 1234"
"0 12"
"0012"
目前我使用以下正则表达式匹配来检查字符串是否有效...
"(|[0-9A-Z\-]{4}| {1}[0-9A-Z\-]{3}| {2}[0-9A-Z\-]{2}| {3}[0-9A-Z\-]{1})"
注意:需要明确的是,上述正则表达式不能满足我的要求,这就是我问这个问题的原因。
我希望有一个更简单的匹配我可以使用,如下所示......
"(| {0,3}[0-9A-Z\-]{1,4})"
我唯一的问题是上面的内容也会匹配这样的
" 1234"
,这是不可接受的。 有没有办法将捕获组限制为只有 4 个字符?
这将通过您所有的测试用例:
"(|[1-9\s][0-9A-Z\s]{2}[0-9A-Z])"
虽然我怀疑有些情况你可能没有提到。
说明:匹配双引号之间的0或4个字符。第一个字符可以是空格或数字,但不能是零。接下来的两个字符是任何数字或大写字母或空格。第四个字符是数字或大写字母,但不是空格。
为了提高效率:
"(?:[A-Z\d-]{4}|[ ](?:[A-Z\d-]{3}|[ ](?:[A-Z\d-]|[ ])[A-Z\d-]))"
https://regex101.com/r/1fr9tb/1
"
(?:
[A-Z\d-]{4}
| [ ]
(?:
[A-Z\d-]{3}
| [ ]
(?: [A-Z\d-] | [ ] )
[A-Z\d-]
)
)
"
基准
Regex1: "(?:[A-Z\d-]{4}|[ ](?:[A-Z\d-]{3}|[ ](?:[A-Z\d-]|[ ])[A-Z\d-]))"
Options: < none >
Completed iterations: 50 / 50 ( x 1000 )
Matches found per iteration: 7
Elapsed Time: 0.66 s, 663.84 ms, 663843 µs
Matches per sec: 527,233
Regex2: "(|[0-9A-Z\-]{4}|[ ]{1}[0-9A-Z\-]{3}|[ ]{2}[0-9A-Z\-]{2}|[ ]{3}[0-9A-Z\-]{1})"
Options: < none >
Completed iterations: 50 / 50 ( x 1000 )
Matches found per iteration: 7
Elapsed Time: 0.94 s, 938.44 ms, 938438 µs
Matches per sec: 372,960
Regex3: "(?="|.{4}")(?![ ]*0)[0-9A-Z -]*"
Options: < none >
Completed iterations: 50 / 50 ( x 1000 )
Matches found per iteration: 6
Elapsed Time: 0.73 s, 728.48 ms, 728484 µs
Matches per sec: 411,814
Regex4: "(|[1-9\s][0-9A-Z\s]{2}[0-9A-Z])"
Options: < none >
Completed iterations: 50 / 50 ( x 1000 )
Matches found per iteration: 6
Elapsed Time: 0.85 s, 851.48 ms, 851481 µs
Matches per sec: 352,327