Home > Software engineering >  Regex to extract time in format HH:MM:SS from string
Regex to extract time in format HH:MM:SS from string

Time:10-31

I have this string

*** Sampled system activity (Sun Oct 31 10:57:41 2021  0100) (1007.04ms elapsed) ***\n

I need to extract 10:57:41

I try with

^([0-1]?\d|2[0-3])(?::([0-5]?\d))?(?::([0-5]?\d))?$

or

^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)$

But it doesn't works

CodePudding user response:

Keeping it simple, I suggest just using:

\b\d{1,2}:\d{2}:\d{2}\b

It is unlikely that anything in your input text other than a time component would match this. Here is a demo showing the pattern working.

CodePudding user response:

Your pattern does not match due to the anchors ^ and $ which assert the start and the end of the string.

If you remove the anchors, you have ([0-1]?\d|2[0-3])(?::([0-5]?\d))?(?::([0-5]?\d))? which will give partial matches as the last 2 groups are optional.

If you remove the ? from the groups, you are left with superfluous non capture groups, which can be omitted as well. Now you have ([0-1]?\d|2[0-3]):([0-5]?\d):([0-5]?\d)

For a match only, you can omit the capture groups, and turn the first capture group in a non capture group. To prevent partial matches, you can add word boundaries \b

The simplified pattern:

\b(?:[01]?\d|2[0-3]):[0-5]?\d:[0-5]?\d\b

Regex demo

  • Related