Sachin Tom
Sachin Tom

Reputation: 29

Regular expression to match time slots

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 –

Upvotes: 0

Views: 340

Answers (1)

JvdV
JvdV

Reputation: 75840

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.

Upvotes: 2

Related Questions