Help on regex

I need a regex for a timespan .. from 15 min to 24 hour. input is something like 00:15

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)

[377 byte] By [GoDaddy] at [2008-2-15]
# 1

Apress:

\d{2}:\d{2}:\d{2}

hour:minute:second

I hope this helps,

wizkid1 at 2007-8-30 > top of Msdn Tech,.NET Development,Regular Expressions...
# 2

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

shakalama at 2007-8-30 > top of Msdn Tech,.NET Development,Regular Expressions...
# 3

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]$

JamesCurran at 2007-8-30 > top of Msdn Tech,.NET Development,Regular Expressions...
# 4

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.

JamesCurran at 2007-8-30 > top of Msdn Tech,.NET Development,Regular Expressions...
# 5
thanks for the lookout.

but it acceptable values should be betwee 00:15 and 23:59

Thanks

GoDaddy at 2007-8-30 > top of Msdn Tech,.NET Development,Regular Expressions...
# 6
Did you ever find an answer to this, or is it still open?
OmegaMan at 2007-8-30 > top of Msdn Tech,.NET Development,Regular Expressions...
# 7

>> 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

JamesCurran at 2007-8-30 > top of Msdn Tech,.NET Development,Regular Expressions...
# 8
Here is a regex where the first two matches say invalidate (don't return a match) if this range is found. The first range is 00:00-00:14 and the second range is (24:00 and up). Then the items are placed in named groups Minutes and Seconds within the match.


(?!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

OmegaMan at 2007-8-30 > top of Msdn Tech,.NET Development,Regular Expressions...

.NET Development

Site Classified