更新时间:2023-01-17 08:35:33
使用
[A-Z]?
使字母可选. {1}
是多余的. (当然,您也可以写[A-Z]{0,1}
,意思是一样的,但这就是?
的用途.)
to make the letter optional. {1}
is redundant. (Of course you could also write [A-Z]{0,1}
which would mean the same, but that's what the ?
is there for.)
您可以将正则表达式改进为
You could improve your regex to
^([0-9]{5})+\s+([A-Z]?)\s+([A-Z])([0-9]{3})([0-9]{3})([A-Z]{3})([A-Z]{3})\s+([A-Z])[0-9]{3}([0-9]{4})([0-9]{2})([0-9]{2})
而且,由于在大多数正则表达式中,\d
与[0-9]
相同:
And, since in most regex dialects, \d
is the same as [0-9]
:
^(\d{5})+\s+([A-Z]?)\s+([A-Z])(\d{3})(\d{3})([A-Z]{3})([A-Z]{3})\s+([A-Z])\d{3}(\d{4})(\d{2})(\d{2})
但是:您真的需要11个单独的捕获组吗?如果是这样,为什么不捕获倒数第四组数字呢?
But: do you really need 11 separate capturing groups? And if so, why don't you capture the fourth-to-last group of digits?