开发者

How to write a regex for MM:DD:YYYY:HH:MM:SS

开发者 https://www.devze.com 2023-02-22 23:46 出处:网络
开发者_开发技巧How can I write a regex for MM:DD:YYYY:HH:MM:SS?If you want to capture the values for month, year, ... you could use something like this, I suppose :
开发者_开发技巧

How can I write a regex for MM:DD:YYYY:HH:MM:SS?


If you want to capture the values for month, year, ... you could use something like this, I suppose :

([0-9]{2}):([0-9]{2}):([0-9]{4}):([0-9]{2}):([0-9]{2}):([0-9]{2})


If supported by your regex engine, \d can be used as an alias to [0-9] :

(\d{2}):(\d{2}):(\d{4}):(\d{2}):(\d{2}):(\d{2})


It depends whether you want to check are all characters divided by : numbers or you also want to check whether you will not get totaly improper data like the 32nd of December or the hour like 25:66. To have such a basic validation you should use a little more complex regexp than the one provided by @Pascal MARTIN, e.g.,

[0-1][0-9]:[0-3][0-9]:[1-2][0-9]{3}:[0-2][0-9]:[0-5][0-9]:[0-5][0-9]
0

精彩评论

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