开发者

What is the shortest notation for this regex?

开发者 https://www.devze.com 2023-03-10 12:31 出处:网络
We\'ve determined that this regex works for validating a certain field, but would like it to be shorter since it will be repeated numerous times:

We've determined that this regex works for validating a certain field, but would like it to be shorter since it will be repeated numerous times:

\D\D\d\d\d\D\d\d\D-\d\d\d 

What is the 开发者_运维问答shortest notation that would still validate the same as the above regex?


Not really shorter, but maybe more readable.

\D{2}\d{3}\D\d{2}\D-\d{3}


I don't know if it's shorter, but this definitely is easier to read:

\D{2}\d{3}\D\d{2}\D-\d{3}


\D\D\d{3}\D\d\d\D-\d{3} - only marginally shorter.

Easier to read, although longer: \D{2}\d{3}\D\d{2}\D-\d{3}


What about

\D{2}\d{3}\D\d{2}\D-\d{3} 


Since \D{2} are 5 chars, this is shorter:

\D\D\d{3}\D\d\d\D-\d{3}


Another solution, same length but groups 3 characters that are repeated twice

\D{2}\d(\d{2}\D){2}-\d{3}
0

精彩评论

暂无评论...
验证码 换一张
取 消