DigitalRay01011
DigitalRay01011

Reputation: 13

Regular expression to match time range 00:00:00

I have dabbled with regex before for simple matches, however I think this is out of my league. I am using Google Analytics (GA) and I want to match Session Durations that come in the format of 00:00:00.

I found some articles similar to what I need but it does not match the range:

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

The problem is I have had many visits that lasted 1 second and some for 1hr in between real visits that lasted say between 10sec and 10mins. Due to the quantity of invalid visits my average is skewed. So I want to add a filter in GA via regex to match times between 00:00:10 and 00:10:00.

Upvotes: 1

Views: 1984

Answers (3)

vianna77
vianna77

Reputation: 505

This may help... an answer without using groups and easy to maintain:

00:10:00|00:0[1-9]:[0-5][\d]|00:00:[1-5]\d

Works with

00:00:00 ignore
00:00:01 ignore
00:00:10 accept
00:00:11 accept
00:00:59 accept
00:00:60 ignore
00:05:03 accept
00:09:59 accept
00:10:00 accept
00:10:01 ignore
00:10:50 ignore
01:20:00 ignore

it will work with eveyrthing between 10 seconds inclusive to 10 minutes inclusive excluding everything else.

Upvotes: 0

Danilo
Danilo

Reputation: 2686

Due to the quantity of invalid visits my average is skewed. So I want to add a filter in GA via regex to match times between 00:00:10 and 00:10:00.

Interpreting this need, try something like this:

^00:(10:00|0[1-9]:[0-5][0-9]|00:[1-5][0-9])$

which is saying:

  • The "hours" part must be 00
  • The "minutes" part can be: EITHER 01 to 09 followed by any second (00 to 59) OR 00 followed by any second between 10 and 59.

The result for a few test values:

00:00:00 NO
00:00:01 NO
00:00:10 YES
00:00:11 YES
00:00:20 YES
00:05:03 YES
00:09:59 YES
00:10:00 YES
00:10:50 NO
01:00:00 NO

Upvotes: 0

Rajeshwar
Rajeshwar

Reputation: 2519

You can use

/^[0-9]{2}:[0-9]{2}:[0-9]{2}$/

OR

/^\d{2}:\d{2}:\d{2}$/

if you want to match only from 00:00:00 to 99:99:99

Here '^' specifies start of pattern and '$' specifies end of pattern. If you don't use them, the pattern will also match '99:99:99:99999', which is not the intended result. So specify them to mention the start and end of the pattern.

If you also wants to match single digit more greater than zero, like 9:9:96 and 01:8:20 etc then use

/^([1-9]{1}|[0-9]{2}):([1-9]{1}|[0-9]{2}):([1-9]{1}|[0-9]{2})$/

Upvotes: 1

Related Questions