Looking to use Regex to filter time information. My idea is as follows:
Sample Inputs:
..."sunday\":[[\"1...
..."sunday\":[[\"2...
..."sunday\":[[\"3...
...
..."sunday\":[[\"9:59...
Essentially, I am looking to filter times that come before 10:00 on Sunday. My data comes in the following format, with the preceding and latter part of the string denoted by "..." as text representing other days of the week. I am looking to create a regex that is able to accomplish this. All of the sample input should pass. Example of input that would fail:
..."sunday\":[[\"11:00...
..."sunday\":[[\"10:01...
..."sunday\":[[\"12:01...
THank you!
Try this .+?sunday.+?"[0-9]:\d+.+
It checks for anything + sunday + anything + an hour format that suits your < 10:00 requirement.
Test it here regex101
Related
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
I have question. I am trying to prepare date regex comparmission. The problem is month and day if its one digit it can be present as 03 or 3 for both month and day. For instance possible values:
2015/03/27 or 2015/4/12 or 2015/07/05 or 2015/2/2 or 2015/02/3
What i did so far is:
^(?<Month>\d(0([0-1]|1[0-2])|([1-12])){1,2})/(?<Day>\d{1,2})/(?<Year>(?:\d{4}|\d{2}))$
I started to make now for month:
(?<Month>\d(0([0-1]|1[0-2])|([1-12])){1,2})
(0([0-1]|1[0-2])|([1-12])){1,2})
so {1,2} - because can be one digit or two for instance (12, 2, 02)
0([0-1]|1[0-2]) | ([1-12])) - because can be two digits or one
somehow i cant figure it into the final version.
Can you help me out?
Using just \d, you might end up with fake dates, like 12/67/4567.
Also, your input has another date format: Year/Month/Day.
I suggest using this regex for your input format:
^(?<Year>(?:19|20)\d{2})\/(?<Month>0?[1-9]|1[0-2])\/(?<Day>3[01]|0?[1-9]|[12][0-9])$
See demo
Optional 0s are made possible due to the ? quantifier after 0.
If it is for .NET, you do not have to escape /s.
To validate the date, use the classes and methods of the programming environment you are using. Here is an example in C#:
var resultFromRegex = "2015/03/27";
DateTime validDate;
var isValid = DateTime.TryParseExact(resultFromRegex, "yyyy/MM/dd", new System.Globalization.CultureInfo("en-US"), System.Globalization.DateTimeStyles.None, out validDate);
Can anyone think of a better way to write this? It works but it is a little ugly.
Input data looks like this: 125100001
The first two numbers are the year, next two are the week number, and last 5 are the serial. I want to validate that the week number is not over 52 for an angular input[number] pattern option. Basically just to leverage the $error field :)
So here it is:
^\d\d(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-2]){1}\d{5}$
Use this:
^(\d{2})([0-4][1-9]|[1-5]0|5[12])(\d{5})$
Notes
The first set of parentheses (0[1-9]|1[0-2]) validates the month: 01-12
The second set of parentheses ([0-4][1-9]|[1-5]0|5[12]) validates the week: 01-52
If you wish, you can retrieve each component with groups 1, 2 and 2
Just for the week part:
[0-4]\d|5[0-2]
so the entire regex would be:
^\d\d([0-4]\d|5[0-2])\d{5}$
So, I have a spinEdit that should display the year and month in this format yyyyMM. I am using RegEx to mask the value to that format but when I want to increment from say 201212 to 201301, it fails and displays 20121. The RegEx I am using looks like this
([0-9][0-9][0-9][0-9])(0[1-9])|(1[0-2])
The issue is that incrementing the value (add 1 to month) isn't incrementing the year field when the month is at 12. The same happens in reverse where decreasing the value (minus 1 month) isn't decreasing the year, 201301 - 1 takes it to 2013. Is there a way to fix this using just RegEx?
I think it is possible, but not fully regex solution, you need to have linux and bash available (personally I find the date function in bash ve) I had to get the date formats (string) in a filename and compare it to a date in a script. Below is the code snippet:
#!/bin/bash
#yyyymm you got after regex
inputdate = 201307
#value you want to subtract
x = 8
#outputdate should return you 201211
outputdate = $(date -d "$inputdate01 -$x month" +"%Y%m")
I believe there may be a way, however that is far to complicated for its worth in a practical situation. So by keeping things simple, it is not possible.
I need a regex for date string which validates
YYYY:MM:DD:HH
YYYY:MM:DD:HH:mm
YYYY:MM:DD:HH:mm:ss
means all 3 formats are valid.
Can someone help me with this ?
I have
d\d\d\d:(0\d|1[012]):([012]\d|3[01]):([01]\d|2[0-3])$ YYYY:MM:DD:HH
^\d\d\d\d:(0\d|1[012]):([012]\d|3[01]):([01]\d|2[0-3]):[0-5]\d$ YYYY:MM:DD:HH:MM
^\d\d\d\d:(0\d|1[012]):([012]\d|3[01]):([01]\d|2[0-3]):[0-5]\d:[0-5]\d$ YYYY:MM:DD:HH:MM:SS
These 3 regex and needs to be combine in one
this is your pattern
YYYY:MM:DD:HH(:mm(:ss)?)?
? means 0 or 1 time
you can test it here
I kept your year month day expression d\d\d\d:(0\d|1[012]):([012]\d|3[01]):([01]\d|2[0-3]). Since your hour and minute expressions where the same :[0-5]\d I just required them to appear zero, once or twice with.
The resulting expression is:
^\d\d\d\d:(0\d|1[012]):([012]\d|3[01]):([01]\d|2[0-3])(:[0-5]\d){0,2}$
This expression by francis-gagnon is a slight modification to prevent edge cases where the day or month is expressed as 00.
^\d\d\d\d:(0[1-9]|1[012]):(0[1-9]|[12]\d|3[01]):([01]\d|2[0-3])(:[0-5]\d){0,2}$
If you're looking to also check the date is valid then you could use something like this monster which will test each date position to it's valid and that the time will fit into 24 hour clock:
^(?:(?:(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(:|\/|-|\.)(?:0?2\1(?:29)))|(?:(?:(?:1[6-9]|[2-9]\d)?\d{2})(:|\/|-|\.)(?:(?:(?:0?[13578]|1[02])\2(?:31))|(?:(?:0?[13-9]|1[0-2])\2(?:29|30))|(?:(?:0?[1-9])|(?:1[0-2]))\2(?:0?[1-9]|1\d|2[0-8]))))(?::(?:[01]\d|2[0-3]))?(?::[0-5]\d){0,2}$
\d{4}:[0-1][0-9]:[0-3][0-9](?::[0-5][0-9](?::[0-5][0-9])?)?