Regular expression to match time range 00:00:00 - regex

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.

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})$/

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.

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

Related

Regex to match all hours except some specific hours

In Google BigQuery, I have to list all tables that contain hours between 02 till 23 hour in their names.
Aka match any hour in this range [02->23], so I created the following regex:
([0-2][2-9])
But the problem is it will skip the hour 10 and 11, and REGEX_EXTRACT returns null value for those “unparsable?” tables.
I could try to match all hours first, then exclude hour 00 and 01. But I could’t find a way in regex to add them as exceptions..
([0-2][0-9])
What would you recommend/suggest in this case? Given that I can’t split this into 2 different regexes.
Thank you.
You could write the pattern as
\b(?:0[2-9]|1\d|2[0-3])\b
Regex demo

REQ: Assistance with Splunk - Rex Query

I'm having some issues with a rex query where a single digit date renders an incorrect result, but a double digit date provides the correct result.
These are the log entries I'm querying:
Mar 7 14:24:29 10.52.176.215 Mar 7 12:24:29 963568 - Melbourne details-cable-issue - vdvfvfv
Mar 20 09:52:55 10.52.176.215 Mar 20 07:52:55 963569 - Brisbane cable-issue
And this is the query:
^(?:[^ \n]* ){7}(?P<extension>[^ ]+)[^\-\n]*\-\s+(?P<location>\w+)
For the Mar 7 entry, my query is giving me group extension "7" whilst my Mar 20 entry is giving me group extension "963569" which is correct.
Can someone shed some light on my query to acknowledge a single and double digit date? #7 vs 20
Thanks all :)
There are several consecutive spaces (they look like padding spaces) in the first string, and since you only match one space within (?:[^ \n]* ) you get mismatches.
I suggest matching 1 or more spaces in that first group and adjusting the limiting quantifier:
^(?:[^ \n]* +){5}(?P<extension>[^ ]+)[^-\n]*-\s+(?P<location>\w+)
^ ^
See the regex demo

Gawk regular expression for days in month

Im writing regular expression that accepts days in months ([0-3])([0-9]). How to change it so it will only accept proper amount of days from 1 to 31, but not 37 like mine... i tried alternation |, but i don't know how to include first group into it.
([0-2])([0-9])|(3)([0-1]) does not work
How to change it so i will have still 2 groups and proper dates?
edit: 2 groups, not 4
Try this :
(0)([1-9])|(1|2)([0-9])|(3)(0|1)
DEMO Match numbers between 01 and 31 only
(0[1-9]|[12][0-9]|3[01])
This accepts values between 0-31 in one group, but does not care about about that February has no days as 30,31.
Sorry, misread it.
If you want to get the values in two groups you have to use negative lookahead like so:
([0-2]|3(?![^0-1]))([0-9])
But I think gawk does not support this.

Datetime Regular Expression Doesn't Work

I am having a hard time reading in string that has date and time in the format:
YYYYMMDDHHmmSS.FFFF[+|-]ZZzz
YYYY is year,
MM is month (starting at 01 to 12),
DD is day (01-31),
HH is hour (00-23),
mm is minute (00-59),
SS is second (00-59),
FFFF is the fraction of the second (0000-9999),
ZZzz is "difference in hours (ZZ – values from +14 to –12) and
minutes (zz – values 00 to 59) from the Coordinated Universal Time
(UTC)."
This is the standard for transferring date time information in HL7, but don't worry about that. The problem I am having is the system handling the regular expression I have written for this standard refuses to let me add the dot following the second field. It also will not allow for the plus or minus prior to the ZZ field.
Here is the regular expression I have written:
/^(1|2)\\d{3}(0[1-9]|1[0-2])(0[1-9]|(1|2)[0-9]|3(0|1))((0|1)[0-9]|2[0-3])[0-5][0-9][0-5][0-9]\\.\\d{4}((\\+|\\-)0[0-9]|\\-1[0-2]|\\+1[0-4])[0-5][0-9]$/
Its for Limesurvey, for its validation field for a given question. If you don't know what that is, just know that its regular expressions use Perl conventions.
Note that if I remove the \., or the \+ \-, it works just fine (with the exception the regex is no longer enforcing the standard). I've also tried not escaping the backslash, but that doesn't do anything either.
If anyone could point to why this isn't working, I would appreciate it. Note that if anything looks odd or redundant in the regex, that's most likely from me logically breaking it into the various fields for easier readability.
I barely changed your regular expression up until the +14 through -12 part. I'm not quite You can see it working here: http://www.regex101.com/r/jF1bA9
Final Regular Expression:
^(1|2)[0-9]{3}(0[1-9]|1[0-2])((0[1-9])|((1|2)[0-9])|3(0|1))((0|1)[0-9]|2[0-3])([0-5][0-9])([0-5][0-9])\.[0-9]{4}(\+0[0-9]|\+1[0-4]|-0[0-9]|-1[0-2])[0-5][0-9]$
Regular Expression explained:
Start of the line:
^ // start of line
Year:
(1|2)[0-9]{3}
Month:
(0[1-9]|1[0-2])
Day:
((0[1-9])|((1|2)[0-9])|3(0|1))
Hour:
((0|1)[0-9]|2[0-3])
Minutes:
([0-5][0-9])
Seconds:
([0-5][0-9])
Period:
\.
Fraction of the second:
[0-9]{4}
Matches +14 through -12 (What you probably need to change)
(\+0[0-9]|\+1[0-4]|-0[0-9]|-1[0-2])
Matches:
+14 +13 +12 +11 +10 +09 +08 +07 +06 +05 +04 +03 +02 +01 +00 -00 -01 -02 -03 -04 -05 -06 -07 -08 -09 -10 -11 -12
00 - 59:
[0-5][0-9]
End of Line:
$
You may need to change it to work with your specific language (I saw you had double backslashes in some areas like \\d)

Regular expression for matching HH:MM time format

I want a regexp for matching time in HH:MM format. Here's what I have, and it works:
^[0-2][0-3]:[0-5][0-9]$
This matches everything from 00:00 to 23:59.
However, I want to change it so 0:00 and 1:00, etc are also matched as well as 00:00 and 01:30. I.e to make the leftmost digit optional, to match HH:MM as well as H:MM.
Any ideas how to make that change? I need this to work in javascript as well as php.
Your original regular expression has flaws: it wouldn't match 04:00 for example.
This may work better:
^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$
Regular Expressions for Time
HH:MM 12-hour format, optional leading 0
/^(0?[1-9]|1[0-2]):[0-5][0-9]$/
HH:MM 12-hour format, optional leading 0, mandatory meridiems (AM/PM)
/((1[0-2]|0?[1-9]):([0-5][0-9]) ?([AaPp][Mm]))/
HH:MM 24-hour with leading 0
/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/
HH:MM 24-hour format, optional leading 0
/^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/
HH:MM:SS 24-hour format with leading 0
/(?:[01]\d|2[0-3]):(?:[0-5]\d):(?:[0-5]\d)/
Reference and Demo
None of the above worked for me.
In the end I used:
^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$ (js engine)
Logic:
The first number (hours) is either:
a number between 0 and 19 --> [0-1]?[0-9] (allowing single digit number)
or
a number between 20 - 23 --> 2[0-3]
the second number (minutes) is always a number between 00 and 59 --> [0-5][0-9] (not allowing a single digit)
You can use this one 24H, seconds are optional
^([0-1]?[0-9]|[2][0-3]):([0-5][0-9])(:[0-5][0-9])?$
The best would be for HH:MM without taking any risk.
^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$
Amazingly I found actually all of these don't quite cover it, as they don't work for shorter format midnight of 0:0 and a few don't work for 00:00 either, I used and tested the following:
^([0-9]|0[0-9]|1?[0-9]|2[0-3]):[0-5]?[0-9]$
You can use this regular expression:
^(2[0-3]|[01]?[0-9]):([1-5]{1}[0-9])$
If you want to exclude 00:00, you can use this expression
^(2[0-3]|[01]?[0-9]):(0[1-9]{1}|[1-5]{1}[0-9])$
Second expression is better option because valid time is 00:01 to 00:59 or 0:01 to 23:59. You can use any of these upon your requirement.
Regex101 link
As you asked the left most bit optional, I have done left most and right most bit optional too, check it out
^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]?$
It matches with
0:0
00:00
00:0
0:00
23:59
01:00
00:59
The live link is available here
None of the above answers worked for me, the following one worked.
"[0-9]{2}:[0-9]{2}"
To validate 24h time, use:
^([0-1]?[0-9]|2?[0-3]|[0-9])[:\-\/]([0-5][0-9]|[0-9])$
This accepts:
22:10
2:10
2/1
...
But does not accept:
25:12
12:61
...
Description
hours:minutes with:
Mandatory am|pm or AM|PM
Mandatory leading zero 05:01 instead of 5:1
Hours from 01 up to 12
Hours does not accept 00 as in 00:16 am
Minutes from 00 up to 59
01:16 am ✅
01:16 AM ✅
01:16 ❌ (misses am|pm)
01:16 Am❌ (am must all be either lower or upper case)
1:16 am ❌ (Hours misses leading zero)
00:16 ❌ (Invalid hours value 00)
Regular Expression
To match single occurrence:
^(0[1-9]|1[0-2]):([0-5][0-9]) ((a|p)m|(A|P)M)$
To match multiple occurrences:
Remove ^ $
(0[1-9]|1[0-2]):([0-5][0-9]) ((a|p)m|(A|P)M)
You can use following regex:
^[0-1][0-9]:[0-5][0-9]$|^[2][0-3]:[0-5][0-9]$|^[2][3]:[0][0]$
Declare
private static final String TIME24HOURS_PATTERN = "([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]";
public boolean validate(final String time) {
pattern = Pattern.compile(TIME24HOURS_PATTERN);
matcher = pattern.matcher(time);
return matcher.matches();
}
This method return "true" when String match with the Regular Expression.
A slight modification to Manish M Demblani's contribution above
handles 4am
(I got rid of the seconds section as I don't need it in my application)
^(([0-1]{0,1}[0-9]( )?(AM|am|aM|Am|PM|pm|pM|Pm))|(([0]?[1-9]|1[0-2])(:|\.)[0-5][0-9]( )?(AM|am|aM|Am|PM|pm|pM|Pm))|(([0]?[0-9]|1[0-9]|2[0-3])(:|\.)[0-5][0-9]))$
handles:
4am
4 am
4:00
4:00am
4:00 pm
4.30 am
etc..
The below regex will help to validate hh:mm format
^([0-1][0-9]|2[0-3]):[0-5][0-9]$
Your code will not work properly as it will not work for 01:00 type formats. You can modify it as follows.
pattern =r"^(0?[1-9]|1[0-2]):[0-5][0-9]$"
Making it less complicated we can use a variable to define our hours limits.Further we can add meridiems for more accurate results.
hours_limit = 12
pattern = r"^[1-hours_limit]:[0-5][0-9]\s?[AaPp][Mm]$"
print(re.search(pattern, "2:59 pm"))
Check this one
/^([0-1]?[0-9]|2[0-3]):([0-5]?[0-9]|5[0-9])$/
Mine is:
^(1?[0-9]|2[0-3]):[0-5][0-9]$
This is much shorter
Got it tested with several example
Match:
00:00
7:43
07:43
19:00
18:23
And doesn't match any invalid instance such as 25:76 etc ...
You can try the following
^\d{1,2}([:.]?\d{1,2})?([ ]?[a|p]m)?$
It can detect the following patterns :
2300
23:00
4 am
4am
4pm
4 pm
04:30pm
04:30 pm
4:30pm
4:30 pm
04.30pm
04.30 pm
4.30pm
4.30 pm
23:59
0000
00:00
check this masterfull timestamp detector regex I built to look for a user-specified timestamp, examples of what it will pickup include, but is most definitely NOT limited to;
8:30-9:40
09:40-09 : 50
09 : 40-09 : 50
09:40 - 09 : 50
08:00to05:00
08 : 00to05 : 00
08:00 to 05:00
8am-09pm
08h00 till 17h00
8pm-5am
08h00,21h00
06pm untill 9am
It'll also pickup many more, as long as the times include digits
Try the following
^([0-2][0-3]:[0-5][0-9])|(0?[0-9]:[0-5][0-9])$
Note: I was assuming the javascript regex engine. If it's different than that please let me know.
You can use following regex :
^[0-2]?[0-3]:[0-5][0-9]$
Only modification I have made is leftmost digit is optional. Rest of the regex is same.