1

I've just tried several hundreds (if not thousands...) of RegEx's available to get what I want... But none of them worked.

I'm simply looking for a Regular expression, that represents a TimeSpan days.hours:minutes:seconds: 7.00:00:00 would represent "7 Days"

This one sadly doesn't work:

(\d\d).(\d\d):(([0-6][0])|([0-5][0-9])):(([0-6][0])|([0-5][1-9]))

SeToY
  • 5,777
  • 12
  • 54
  • 94

5 Answers5

3

This one is technically more correct and probably more efficient too:

\d+\.((0?\d)|(1\d)|(2[0-3]))(:[0-5]\d){2}
Bas
  • 26,772
  • 8
  • 53
  • 86
dotNET
  • 33,414
  • 24
  • 162
  • 251
2

The accepted answer doesn't seem to work for the basic case.

Following the TimeSpan parsing rule I have come up with this pattern.

^((?:-?0*\d+\.)?(?:0*)(?:2[0-3]|1[0-9]|[0-9]))(?::0*([0-5]?[0-9]))?(?::0*((?:[0-5]?[0-9])(?:\.\d{0,7})?))?$

Note This pattern will pass the 10.12 format while MSDN marks this as an invalid format. This should be a valid format IMO.

Tri Q Tran
  • 5,500
  • 2
  • 37
  • 58
  • 1
    This is a format that completely conform TimeSpan parsing rule: `^((((?:-?0*\d+\.)?(?:0*)(?:2[0-3]|1[0-9]|[0-9]))(?::0*([0-5]?[0-9]))?(?::0*((?:[0-5]?[0-9])(?:\.\d{0,7})?)))|(\d+))?$`. You can ask them to support `10.12` format as well but for now regex must satisfy the same rules – Alex Zhukovskiy Nov 28 '19 at 10:52
2

Regex below recognize "[-][[dd.]HH:]mm[:ss[.fffffff]]" pattern and use named groups:

(?:(?<ne>-))?(?:(?:(?<dd>0*[0-9]+)[.])?(?:(?<HH>0*[2][0-3]|0*[1][0-9]|0*[0-9])[:]))?(?<mm>(?<=:)0*[0-5]?[0-9]|0*[5-9]?[0-9](?=[:]))(?:[:](?<ss>0*[0-5]?[0-9](?:[.][0-9]{0,7})?))?

C# Code:

/// <summary>Time Span Standard regular expression.</summary>
/// <remarks>
/// Minutes are mandatory with required colon from left or right.
/// Pattern: [-][[dd.]HH:](:mm|mm:)[:ss[.fffffff]]
/// </remarks>
public const string TimeSpanStandard =
    @"(?:(?<ne>-))?" +
    @"(?:(?:(?<dd>0*[0-9]+)[.])?(?:(?<HH>0*[2][0-3]|0*[1][0-9]|0*[0-9])[:]))?" +
    @"(?<mm>(?<=:)0*[0-5]?[0-9]|0*[5-9]?[0-9](?=[:]))" +
    @"(?:[:](?<ss>0*[0-5]?[0-9](?:[.][0-9]{0,7})?))?";

Note: This pattern treats 'dd.HH' as an invalid format. This works for me, because I use this regex to find and replace TimeSpan in JSON strings and support of 'dd.HH' format could wrongly pick up normal numbers.

Regex value example:

-01.23:59:30.999999 - ✔

    Match  1   0-19 -01.23:59:30.999999
    Group ne   0-1  -
    Group dd   1-3  01
    Group HH   4-6  23
    Group mm   7-9  59
    Group ss 10-19  30.999999

Regex Pass:

    01.02:03:04.05 - ✔
    01.02:03:04 - ✔
    01.02:03 - ✔
    02:03:04.05 - ✔
       03:04.05 - ✔
    02:03 - ✔
    02:03:04 - ✔

Regex Fail:

    1.03:04.05 - ✗
         04.05 - ✗
Evaldas Jocys
  • 169
  • 1
  • 3
1

That's because your Regex pattern is expecting 2 digits for days and you only have 1 digit. Just make the first digit optional with ?

(\d?\d)\.(\d\d):(60|([0-5][0-9])):(60|([0-5][0-9]))

or better yet just use + to match one or more because that pattern would still not match 100 days

(\d+)\.(\d\d):(60|([0-5][0-9])):(60|([0-5][0-9]))
Nick
  • 4,556
  • 3
  • 29
  • 53
0

This one worked pretty well for me.

^(\d{1,2}|\d\.\d{2}):([0-5]\d):([0-5]\d)(\.\d+)?$

Here are the validation results for different inputs.

+-----------------------+-------+
| Input                 | Valid |
+-----------------------+-------+
| 1.10:14:15.1          | True  |
+-----------------------+-------+
| .10:14:15.1           | False |
+-----------------------+-------+
| 1.10:14:15.           | False |
+-----------------------+-------+
| 1.10:14:15.123haha456 | False |
+-----------------------+-------+
| 10:14:15.1            | True  |
+-----------------------+-------+
| 100:14:15.1           | False |
+-----------------------+-------+
| 6:14:15.1             | True  |
+-----------------------+-------+
| :14:15.1              | False |
+-----------------------+-------+
| 00:14:15              | True  |
+-----------------------+-------+
Steztric
  • 2,832
  • 2
  • 24
  • 43