Search code examples
javascriptregexstringamazon-lex

Detect and parse a time range in a format like "2-4pm" for Amazon Lex scheduling bot


Short version: Given user input like I'm going to be out of the office from 2-4pm or I'm out of the office from 10am-noon tomorrow, how can I isolate the time range so I can split it on the dash and process it as a Start and End time?

Long version in case another solution would work:

I'm working on an Amazon Lex bot for out of office scheduling. My utterances are set up in phrases similar to I'll be out of the office from {StartTime} to {EndTime} which are working great if users type the times in a format like 2pm to 4pm. However, some users are saying I'll be out of the office from 2-4pm or even worse 10am-noon. Because Lex utterances are required to be formatted like sentences that can be spoken aloud to Alexa etc., it won't let you use dashes, so I can't create an utterance like {StartTime}-{EndTime} to tell it to expect dates in that format.

I already have a Lambda validation function set up so I can do custom processing on the user's input, but I'm not sure how to isolate that piece of the string so I can parse it into my StartTime and EndTime slots.


Solution

  • if you are looking for the regex:

    \b(\w+)\s*-\s*(\w+)\b
    

    Demo