Regular expression to match time slots - regex

I am dealing with a patient day calendar with time slots.
Each hour is divided into two equal time slots.
If the person is available for the whole 1 hour it is is denoted as 11.
If not available for the whole hour : 00.
Available for first half only : 10 .
Available for second half only : 01 .
So the entire calendar of the patient has 48 digits
But for now, just to ease things, we will assume we are looking from 12AM to 6AM only.
So the calendar will be [01]{12}
Suppose the patient is available from 12AM to 3AM and 4AM to 6AM.
Now his calendar can be marked as
111111001111
Now the doctors' calendar is also stored in the similar way
Now suppose the appointment window is fixed as 1 hour, I need to find all the doctors having a 1 hour available timeslots within patient's available timeslot
The doctors' availability outside the patient's available time doesn't matter, ie in this case 3AM to 4AM
Basically, I want to write a regex to match all those
I tried (^([01]{2})(?=(?:[01]{0,2}11))([01]{4})|^([01]{8})(?=(?:[01]{0,2}11))([01]{2})) but this is giving match for any doctor who dont have any free slot in patient's availablilty but any 1 hour free slot after 6AM also –

The following seems to validate all the schedules from 12AM to 6AM where doctor have at least an hour available in either the timeslot 12AM to 3Am or 4AM to 6AM:
^(?=(?:[01]{0,4}|[01]{8,10})11)[01]{12}$
See the online demo
^ - Start line anchor.
(?= - Open positive lookahead:
(?: - Open non-capture group to write out alternations:
[01]{0,4} - Assert position is followed by 0-4 times a zero or one.
| - Or:
[01]{8,10} - Assert position is followed by 8-10 times a zero or one.
) - Close non-capture group.
11 - Literally match "11" which represents an available hour.
) - Close positive lookahead.
[01]{12} - Match a zero or 1, exactly 12 times.
$ - End string anchor.

Related

Capturing the digit in a new line after whitespace

The string we have right now is:
DB GOALS: DISADVANTAGED BUSINESS ENTERPRISE - 6.0%
PROPOSALS ISSUED 9 FUND TOTAL , , 0
TOTAL NUMBER OF WORKING DAYS 30
NUMBER OF BIDDERS 4 ENGINEERS EST 1,674,885.00 AMOUNT OVER
177,014.00 PERCENT OVER EST 10.57
PROGRAM ELEMENTS
I am using the pattern (AMOUNT OVER|AMOUNT UNDER)[\n\r\s]+(?:^|\s)(?=.)((?:0|(?:[1-9](?:\d*|\d{0,2}(?:,\d{3})*)))?(?:\.\d*[0-9])?)(?!\S) but it does not capture
AMOUNT OVER
177,014.00
in the text. I suspect it is because of the whitespace before 177,014.00 because it works when we remove the whitespace.
Is there a way to capture it as it is? Thanks so much!
Here is the regex101.com link for reference.
You might simplify the pattern a bit to:
\b(AMOUNT (?:OVER|UNDER))\s+((?:\d{1,3}(?:,\d{3})*(?:\.\d\d)?))(?!\S)
Note that [\n\r\s]+ can be written as \s+
Regex demo

Regex to check MM:SS [duplicate]

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.

Regex MM:SS, unlimited minutes (no hours)

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.

Complex Regex finding date and time

Is there someone to help me with the following:
I'm trying to find specific date and time strings in a text (to be used within VBA Word).
Currently working with the following RegEx string:
(?:([0-9]{1,2})[ |-])?(?:(jan(?:uari)?|feb(?:ruari)?|m(?:aa)?rt|apr(?:il)?|mei|jun(?:i)?|jul(?:i)?|aug(?:ustus)?|sep(?:tember|t)?|okt(?:ober)?|nov(?:ember)?|dec(?:ember)?))?(?: |-)?(?(3)(?: around | at | ))?(?:([0-9]{1,2}:[0-9]{1,2})?(?: uur| u|u)?)?
Tested output on following text:
date with around time: 26 sep 2016 around 09:00u
date with at time: 1 sep 2016 at 09:00 uur
date and time u: 1 sep 2018 09:00 u
time without date: 08:30 uur
date with time u: 1 sep 2016 at 09:00u
only time: 09:00
only month: jan
month and year: feb 2019
only day: 02
only day with '-': 2-
day and month: 2 jan
month year: jan 2018
date with '-': 2-feb-2018 09:00
other month: 01 sept 2016
full month: 1 september 2018
shortened year: jul '18
Rules:
a date followed by time is valid
a date followed by text 'around' or 'at', followed by time is valid
a date without day number is valid
a date without year is valid
a date, month only is not valid
a day, without month or year not valid
a date may contain dashes '-'
a year may be shortenend with ', like jun '18
month name can be short or long
full match includes ' uur' or 'u' (to highlight the text in ms-Word)
submatches text from capture are without prepending or trailing spaces
example at: [https://regex101.com/r/6CFgBP/1/]
Expected output (when using in VBA Word):
An regex Matches collection object in which each Match.SubMatches contains the individual items d, m, y, hh:mm from the capture groups in the regex search string.
So for example 1: the Submatches (or capture groups) contains values: '26' ','sep','2016','09:00'
The RegEx works fine, but some false-positives need to be excluded:
In case there is a day without month/year, should be excluded from Regex (example 9 and 10)
In case there is a month without day, should be excluded (example 7)
(I was trying with som lookahead and reference \1 and ?(1), but was not able to get it running properly...)
Any advice highly appreciated!
As I understood, you require that each date/time part (day, month, year, hour
and minute) must be present.
So you should remove ? after relevant groups (they are not optional).
It is also a good practice to have each group captured as a relevant capturing group.
There is no need to write something like jun(?:i)?. It is enough
(and easier to read) when you write just juni? (the ? refers just
to preceding i).
Another hint: As the regex language contains \d char class, use just
it instead of [0-9] (the regex is shorter and easier to read.
Optional parts (at / around) should be an optional and non-capturing group.
Anything after the minute part is not needed in the regex.
So I propose a regex like below (for readability, I divided it into rows):
(\d{1,2})[ -](jan(?:uari)?|feb(?:ruari)?|m(?:aa)?rt|apr(?:il)?|mei|juni?
|juli?|aug(?:ustus)?|sep(?:tember|t)?|okt(?:ober)?|nov(?:ember)?|dec(?:ember)?)
[ -](\d{4}) (?:around |at )?(\d{1,2}:\d{1,2})
Details:
(\d{1,2}) - Day.
[ -] - A separator after the day (either a space or a minus).
(jan(?:uari)?|...dec(?:ember)?) - Month.
[ -] - A separator after the month.
(\d{4}) - year.
(?:around |at )? - Actually, 3 variants of a separator between year
and hour (space / around / at), note the space before (...)?.
(\d{1,2}:\d{1,2}) - Hour and minute.
It matches variants 1, 2, 3, 5 and 13.
All remaining fail to contain each required part, so they are not matched.
If you allow e.g. that the hour/minute part is optional, change the respective fragment
into:
( (?:around |at )?(\d{1,2}:\d{1,2}))?
i.e. surround the space/around/at / hour / minute part with ( and )?,
making this part an optional group. Then, variants 14 and 15 will also
be matched.
One more extension: If you also allow the hour/minute part alone,
add |(\d{1,2}:\d{1,2}) to the regex (all before is the first variant and
the added part is the second variant for just hour/minute.
Then, your variants No 4 and 6 will also be matched.
For a working example see https://regex101.com/r/33t1ps/1
Edit
Following your list of rules, I propose the following regex:
(\d{1,2}[ -])? - Day + separator, optional.
(jan(?:uari)?|...|dec(?:ember)?) - Month.
(?:[ -](\d{4}|'\d{2}))? - Separator + year (either 4 or 2 digits with "'").
( (?:around |at )?(\d{1,2}:\d{1,2}))? - Separator + hour/minute -
optional end of variant 1.
|(\d{1,2}:\d{1,2}) - Variant 2 - only hour and minute.
It does not match only your variants No 9 and 10.
For full regex, including also "uur" see https://regex101.com/r/33t1ps/3
Finally I found something that helps me using the month properly :-)
\b(?:([1-3]|[0-3]\d)[ |-](?'month'(?:[1-9]|\d[12])|(?:jan(?:uari)?|feb(?:ruari)?|m(?:aa)?rt|apr(?:il)?|mei|jun(?:i)?|jul(?:i)?|aug(?:ustus)?|sep(?:tember|t)?|okt(?:ober)?|nov(?:ember)?|dec(?:ember)?))?)?(?:(\g'month')[ |-]((?:19|20|\')(?:\d{2})))?\b(?: omstreeks | om | )?(?:(\d{1,2}[:]\d{2}(?: uur|u)?|[0-2]\d{3}(?: uur|u)))?\b
It uses a named constructor/subroutine. Found here:
https://www.regular-expressions.info/subroutine.html

Conditional Regex Parsing

I have bunch of product codes that I'm trying to parse (Example 99 ITEM SEC SALE). In rare conditions, product codes are like 99 ITEM SEC SALE.
If it the cell is "99 ITEM SEC SALE" then "ITEM SEC" will be parsed (take out 99 and SALE).
If the cell is "99 ITEM SEC" (NO--> SALE,SOLD, OR PURCHASED). I want ITEM SEC will be parsed as well.In other words, "SALE SOLD AND PURCHASED" are prohibited words.
1-It always starts with a set of numbers (no limit)
2-Alphabetic characters (Any)
3-Alphabetic characters (any)-optional
4-If the ending value(string) is NOT "SALE" or "SOLD" or "PURCHASED" then take the digits out and parse
I found something similar but could not figure out how it should work for my case.
Thanks for the help
Okay, so what you're working for is something like this.
(?P<number>\d+)\s+(?P<Item_Name>\w+)\s+(?P<code>[a-zA-Z]{0,3})\s+(?P<status>SOLD|SALE|PURCHASED)?
(?P<number>\d+) -- Named Capture Group 1 (number)- Match any number
\s+ -- Match any number of spaces
(?P<Item_Name>\w+) -- Named Capture Group 2 (Item_Name) - Match any word until space
\s+ match any number of spaces
(?P<code>[a-zA-Z]{0,3}) -- Named Capture Group 3 (code) - Match any a-zA-Z character 0-3 times
\s+ match any number of spaces
(?P<status>SOLD|SALE|PURCHASED)? -- Named Capture Group 4 (status) - Match SOLD / SALE / PURCHASED (? means 0 or 1 times so this is optional)
Live example: https://regex101.com/r/oR3sK8/1
I don't recall if named capture groups work like this for objective-C, if they don't you can remove the ?P<...> and the regex should still operate without issues (and keep your capture groups largely unchanged).