I am trying to compose a regExp that accepts HH:mm time formats, but also accepts all of the intermediate values:
e.g. all of these are accepted:
0
1
12
12:
12:3
12:30
1:
1:3
1:30
For now, I came up with this: ^([\d]{1,2}):?([\d]{1,2})?$
But this accepts any numeric 1/2 digit values for hours and minutes (e.g. 25:66 is acceptable)
So I came relatively close to my goal, but I need to filter out values x>24 from the hours, and x>60 from the minutes?
Try this:
^((?:[01][0-9]?)|(?:2[0-4]?)|(?:[3-9]))(?::((?:[0-5][0-9]?)|(?:60))|:)?$
NOTE:
This accepts 24 for HH and 60 for MM as stated in your question:
but I need to filter out values x>24 from the hours, and x>60 from the minutes?
Thus ff. are accepted:
0
1
12
12:
12:3
12:30
1:
1:3
1:30
1:60
24:60
24:00
00:60
and below are not accepted:
25:30
00:61
Regex DEMO 1
If you want to exclude 24 HH and 60 MM, try this instead:
^((?:[01]\d?)|(?:2[0-3]?))(?::|(?::([0-5][0-9]?)))?$
Regex DEMO 2
Groups (applies to both cases):
\1 = HH
\2 = MM
You are looking for
^(?:[01]\d?|2[0-3]?)(?::(?:[0-5]\d?)?)?$
See the regex demo and the regex graph:
Details:
^ - start of string
(?:[01]\d?|2[0-3]?) - either a 0 or 1 followed by an optional digit, or a 2 followed with an optional 0, 1, 2 or 3
(?::(?:[0-5]\d?)?)? - an optional sequence of patterns:
: - a colon
(?:[0-5]\d?)? - an optional sequence of patterns:
[0-5] - a digit from 1 to 5
\d? - an optional digit
$ - end of string.
Related
I'm using some validation in Google Sheets but I need the total minutes and seconds in the following format for each dive.
E.g.
42:38 (42 minutes, 38 seconds)
62:35
85:26
I do not want hours to be an option; I only want to work with minutes and seconds, as this is then converted in the database separately.
I have seen the below on another form but have no idea where to start with regex:
^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)$
This gives HH:MM:SS (with HH being optional); I have tried to have a play around with it but to no avail.
You can use
^(\d+):([0-5]?\d)$
Or, if \d is not supported as in POSIX ERE:
^([0-9]+):([0-5]?[0-9])$
See the regex demo. Details:
^ - start of string
(\d+) - Group 1: one or more digits
: - a colon
([0-5]?\d) - Group 2: a digit from 0 to 5 and then one digit
$ - end of string.
I'm using some validation in Google Sheets but I need the total minutes and seconds in the following format for each dive.
E.g.
42:38 (42 minutes, 38 seconds)
62:35
85:26
I do not want hours to be an option; I only want to work with minutes and seconds, as this is then converted in the database separately.
I have seen the below on another form but have no idea where to start with regex:
^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)$
This gives HH:MM:SS (with HH being optional); I have tried to have a play around with it but to no avail.
You can use
^(\d+):([0-5]?\d)$
Or, if \d is not supported as in POSIX ERE:
^([0-9]+):([0-5]?[0-9])$
See the regex demo. Details:
^ - start of string
(\d+) - Group 1: one or more digits
: - a colon
([0-5]?\d) - Group 2: a digit from 0 to 5 and then one digit
$ - end of string.
We've a "street_number" field which has been freely filed over the years that we want to format. Using regular expressions, we'd like to to extract the real "street_number", and the "street_number_suffix".
Ex: 17 b, "street_number" would be 17, and "street_number_suffix" would be b.
As there's a dozen of different patterns, I'm having troubles to tune the regular expression correctly. I consider using 2 different regexes, one to extract the "street_number", and another to extract the "street_number_suffix"
Here's an exhaustive set of patterns we'd like to format and the expected output:
# Extract street_number using PCRE
input street_number street_number_suffix
19-21 19 null
2 G 2 G
A null A
1 bis 1 bis
3 C 3 C
N°10 10 null
17 b 17 b
76 B 76 B
7 ter 7 ter
9/11 9 null
21.3 21 3
42 42 null
I know I could invoke an expressions that matches any digits until a hyphen using \d+(?=\-).
It could be extended to match until a hyphen OR a slash using \d+(?=\-|\/), thought, once I include \s to this pattern, 21 from 19-21 will match. Adding conditions may no be that simple, which is why I ask your help.
Could anyone give me a helping hand on this ? If it can help, here's a draft: https://regex101.com/r/jGK5Sa/4
Edit: at the time I'm editing, here's the closest regex I could find:
(?:(N°|(?<!\-|\/|\.|[a-z]|.{1})))\d+
Thought the full match of N°10 isn't 10 but N°10 (and our ETL doesn't support capturing groups, so I can't use /......(\d+)/)
To get the street numbers, you could update the pattern to:
(?<![-/.a-z\d])\d+
Explanation
(?<! Negative lookbehind
[-/.a-z\d] Match any of the listed using a charater class
) Close the negative lookbehind
\d+ Match 1+ digits
Regex demo
VB2010: I have text that consists of blocks of text that start with day and time DD HHMM and end only at the next day/time.
Here is my sample text:
18 2131 Z50000 ZZ-AAA
PR
PR
AGM TPS P773QQ 1500 DCA 22FEB
21,77,23,M10,F,26,3100,2
OK
18 2134 Z50000 ZZ-AAA
PR
QU HMKKDBB
.DDVZAZC 182134
ARR
FI US1500/AN P773QQ/DA KDCA/AD KMIA/IN 2026/FB 152/LA /LR
DT DDL DCAV 182134 M33A
- OS KMIA /GNO6541/R200RR
18 2134 Z50000 ZZ-AAA
PR
PR
ARR OPN P773QQ 1500 DCA 22FEB
0757
OK
18 2135 Z50000 ZZ-AAA
PR
PR
ARR M58 P773QQ 1500 DCA 22FEB
212
UNKNOWN POL/SPOL
QU HMKKDBB
.DDVZAZC 182134
ARR
FI US1500/AN P773QQ/DA KDCA/AD KMIA/IN 2026/FB 152/LA /LR
DT DDL DCAV 182134 M33A
- OS KMIA /GNO6541/R200RR
18 2136 Z50000 ZZ-AAA
PRF 1500/18 MIA IN 0152 333
18 2137 Z50000 ZZ-AAA
PR
PRZ 1500/18 MIA IN 2026 N/A 333
My goal is to get only the blocks of text that have key phrases ^FI and ^DT in the middle. The matching groups should contain only two blocks. The one from 18 2134 and end at M33A and then from 18 2135 to M33A.
I have tried:
This works for the most part except it starts the match at the prior block.
RegexOptions.Singleline Or RegexOptions.Multiline Or RegexOptions.IgnoreCase
^\d\d \d{4}(.*?)^FI US(.*?)^DT DDL(.*?)\r
This one I took from another post but cant seem to wrap my head around. It matches only the first part of every block.
RegexOptions.Multiline Or RegexOptions.IgnoreCase
^\d\d \d{4}.*\r[\s\S]*?(?=(?:^\d\d \d{4}|$))
Haven't used regex in a while so any help appreciated.
You may use
(?ms)^\d\d +\d{4}\b(?:(?!^(?:\d\d +\d{4}\b|FI|DT)).)*?^(?:FI|DT).*?(?=^\d\d +\d{4}\b|\Z)
See the regex demo (Though it is a PCRE regex test, it will work the same in .NET).
Pattern details
(?ms) - multiline and singleline options
^ - start of a line
\d\d +\d{4}\b - 2 digits, 1 or more spaces and 4 digits as a whole word
(?:(?!^(?:\d\d +\d{4}\b|FI|DT)).)*? - any char, 0+ repetitions, as few as possible, that does not start the sequence: start of a line, 2 digits, 1 or more spaces and 4 digits as a whole word, or FI or DT
^(?:FI|DT) - FI or DT at the start of a line
.*? - any 0+ chars, as few as possible
(?=^\d\d +\d{4}\b|\Z) - a positive lookahead that requires ^\d\d +\d{4}\b (start of a line, 2 digits, 1 or more spaces and 4 digits as a whole word) or \Z (end of string) to match immediately to the right of the current location.
This regex should find what you need, if single line enabled
[0-3]\d\s+[0-2]\d[0-5]\d.*?(FI.*?)\n(DT.*?)\n
Explanation:
[0-3]\d\s+[0-2]\d[0-5]\d day hour and minute check
.*? ungreedy capturing, . includes newline
(FI.*?)\n first group, FI line, until line break
(DT.*?)\n second group, same deal
I want to parse a hh:mm:ss string.
A simple one is ([0-1]?\d|2[0-3]):([0-5]?\d):([0-5]?\d) which expects 2:3:24 or 02:03:24 string.
I want to take it a step further and pass the validation even in cases like
if you enter just 56, it should be pass, as 56 can be considered as 56 secs [SS]
if you enter 2:3 or 02:03 or 02:3 or 2:03 it should pass. 2 minutes and 3 seconds [MM:SS]
If you enter 20:30:12 pass with 20 hrs, 30 minutes and 12 secs [HH:MM:SS]
if you enter 78:12 , do not pass 78 minutes is wrong....
Basically, if one ":" is found, consider number before ":" as MM and number after ":" as SS
. If two ":" are found consider as HH:MM:SS
I came up with this pattern.
(^([0-1]?\d|2[0-3]):([0-5]?\d):([0-5]?\d)$)|(^([0-5]?\d):([0-5]?\d)$)|(^[0-5]?\d$)
It seems to be working fine. I wanted to know any other simpler regular expression, that can do the job.
^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)$
Explanation:
^ # Start of string
(?: # Try to match...
(?: # Try to match...
([01]?\d|2[0-3]): # HH:
)? # (optionally).
([0-5]?\d): # MM: (required)
)? # (entire group optional, so either HH:MM:, MM: or nothing)
([0-5]?\d) # SS (required)
$ # End of string
#Tim Pietzcker covers the OP's requirement for a HH:MM:SS parser where SS was mandatory, i.e.
HH:MM:SS
MM:SS
SS
If you permit me to deviate from the OP's requirement for a bit, and consider a case where HH is mandatory, i.e.
HH
HH:MM
HH:MM:SS
The regex I came up with was:
^([0-1]?\d|2[0-3])(?::([0-5]?\d))?(?::([0-5]?\d))?$
Let's break it down:
([0-1]?\d|2[0-3]) - matches for hours
(?::([0-5]?\d))? - optionally matches for minutes
(?::([0-5]?\d))? - optionally matches for seconds