Regex and Timespan
I'm not very good in building the pattern.
Thanks,
(Moderator: Thread move to Regular Expression forum and Title changed from "Help on regex" to "Regex and Timespan" for quicker thread understanding during a search)
I'm not very good in building the pattern.
Thanks,
(Moderator: Thread move to Regular Expression forum and Title changed from "Help on regex" to "Regex and Timespan" for quicker thread understanding during a search)
hi,
a tool to help you build your own regex http://www.codeproject.com/dotnet/expresso.asp
regular expression tutorial
http://www.codeproject.com/dotnet/regextutorial.asp
hope this helps
Now how exact to you want this? So far, you've only given us examples of two good value. To write a regex pattern, we really need to know what you want to reject. Is any time good, or are we only accepting 1/4 hour increments? Is 24 hours a hard limit, or is is an upper limit of 29:59:59 acceptable (it would make things a whole lot easier)
If you'll accept any value from 0:00 to 29:59 than this should work:
[012]?[0-9]:[0-5][0-9]$
TskTskTsk
a) He didn't ask for seconds, and
b) It would seem that "98:87:65", while acceptable to your regex, is not a good timespan value.
>> but it acceptable values should be betwee 00:15 and 23:59
IF that's exactly what you want (00:10 is bad, but 00:16 & 01:01 is good) then this would be a bit tricky from a Regex, so you may want to consider a different approach.
However, This should work:
"(00:1[5-9])|(00:[2-5][0-9])|(0[1-9]:[0-5][0-9])|(1[0-9]:[0-5][0-9])|(2[0-3]:[0-5][0-9])"
It get the following result:
Matching: 00:10 No Match
Matching: 00:15 1 => 00:15
Matching: 00:16 1 => 00:16
Matching: 01:01 3 => 01:01
Matching: 01:20 3 => 01:20
Matching: 17:47 4 => 17:47
Matching: 17:74 No Match
Matching: 23:59 5 => 23:59
Matching: 24:15 No Match
And Regular Expression Workbench gives the following interpretatiuon:
Capture
00:1
Any character in "5-9"
End Capture
or
Capture
00:
Any character in "2-5"
Any character in "0-9"
End Capture
or
Capture
0
Any character in "1-9"
:
Any character in "0-5"
Any character in "0-9"
End Capture
or
Capture
1
Any character in "0-9"
:
Any character in "0-5"
Any character in "0-9"
End Capture
or
Capture
2
Any character in "0-3"
:
Any character in "0-5"
Any character in "0-9"
End Capture
(?!00\:[0|1][0-4])(?![2-9][4-9]:\d{2})(?<Minutes>[0-2]\d)(?:\:)(?<Seconds>[0-5]\d) |
Tested on these numbers, matches in green
00:14
00:15
00:59
00:60
17:47
17:74
23:59
24:15
30:00