开发者

Regex Hour:Minute (till 31:59)

开发者 https://www.devze.com 2022-12-16 13:41 出处:网络
I need to identify a hour in a string in a Regex. The rules are the following: The hour is in the format of %m or %h:%m (by %x I mean xx or x);

I need to identify a hour in a string in a Regex. The rules are the following:

  • The hour is in the format of %m or %h:%m (by %x I mean xx or x);
  • The hour should be till 31:59;
  • Should NOT pass strings like 000, 25:545; :12;

So, my actual variant of Regex is (with ExplicitCapture option, I verify it here):

((?<hours>([012]*[0-9])|([3]*[01]))\:)*(?<minutes>开发者_JS百科[0-5]*[0-9])

The problem is that I don't achieve to "limit" this string ($myregex^, surprisingly, stopped detecting a valid string). Any idea why?


Try this:

^((?<hours>([012]?\d)|(3[01])):)?(?<minutes>[0-5]?\d)$

You have some problems with your regex. Mostly, you're using * where you shouldn't. This opens the door to many errors. For example, it matches the string "0010203333333".

0

精彩评论

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