Regex to capture the time range formats listed below [Need optimized solution]

Hi Team,

This is regarding the the process capturing the instances of multiple time formats from the string into list/array.

For example strInput = “Today’s meeting can be from 07:00 AM - 02:00 PM or 6am-2pm or 6am-11AM”
Then capture 3 instances of times into a string array\list variable.

List of the strings formats of time range to check are:
07:00 AM - 02:00 PM
07:00 am - 02:00 pm
07:00 AM to 02:00 PM
07:00 am to 02:00 pm
8:00 am to 3:00 pm
8:00 AM to 3:00 PM
7AM - 2PM
7am - 2pm
6am-2pm
6AM-2PM
7:00AM-3:30PM
7:00AM-3:00PM
6am-11am
6AM-11PM
7:30am to 2pm
7:30AM to 2PM
6AM to 3PM
6am to 3pm

Thanks and regards,
@hacky

HI @hacky

How about this expression?

System.Text.RegularExpressions.Regex.Match(YourString,"\d\S.+(?<=M|m)").Tostring

Regards
Gokul

give a try at following:

  • we did set the RegexOption IgnoreCase / CaseInsensitive
  • it is targeted to catch the from to pairs
    From the match we can refer to the groups for getting the from/to info:

or to the match for getting all

Pattern: ([\d\:]+\s?(AM|PM)?).*?([\d\:]+\s?(AM|PM)?)

@ppr , @Gokul001

Really sorry for not clarifying earlier, but please note that there shall be text before and after the extracted time formats. And we require solution to be able to extract the mentioned time formats from series of strings:

  • Today is 07:00 AM - 02:00 PM November month
  • this 07:00 am - 02:00 pm is for testing
  • hello 07:00 AM to 02:00 PM world
  • this 07:00 am to 02:00 pm you
  • random 8:00 am to 3:00 pm text
  • I dont know 8:00 AM to 3:00 PM text
  • text 1 7AM - 2PM text
  • Hello 7am - 2pm world
  • A-B-c 6am-2pm text123
  • I one 6AM-2PM text
  • Helo 7:00AM-3:30PM world
  • 7:00AM-3:00PM Hello
  • 6am-11am This is for testing
  • solution 6AM-11PM Knowing
  • 7:30am to 2pm text
  • 7:30AM to 2PM string
  • 6AM to 3PM variable
  • 6am to 3pm new
  • 8:00-3:00pm array
  • 8:00-3:00PM string

HI @hacky

How about this expression?

System.Text.RegularExpressions.Regex.Match(YourString,"\d\S.+(?<=PM|pm|Pm|am|Am|AM)").Tostring

Regards
Gokul

@Gokul001 @ppr @Palaniyappan @supermanPunch

There are some text characters which actually has the words endingwith ‘PM or AM’ which is messing up the logic. I also tried adding space after AM/PM but that is not helping either.

Free texts are human inputted and have very weird variations to catering the list of time range formats.

Not getting what shall be the best solution around it.