Grouping timestamps with descriptions

I have data for tasks that were recorded with a time sheet app. I’m trying to parse the breaks for each task.

An example break string attached to a task can look like this:

1:19pm – 10:33pm ate tacos 10:35pm – 11:38pm 12:40am – 1:24am took a nap

I need to group this into time stamps with their associated descriptions. The above should be grouped like:

1:19pm – 10:33pm ate tacos

10:35pm – 11:38pm

12:40am – 1:24am took a nap

The description: “Grouping timestamps with descriptions”t have descriptions.

I figure regex would be the simplest way to get an array of intervals with their descriptions (if they have one).

So far I have:

\d{1,2}:\d{2}[ap]m\s–\s\d{1,2}:\d{2}[ap]m

which matches the time stamps 1:19pm – 10:33pm , 10:35pm – 11:38pm , and 12:40am – 1:24am

I am using JavaScript, and the match function, to parse this data. I want to make a regular expression that will match the time stamp and everything that follows it until the next time stamp.

I’m a beginner with regex so go easy on me. I’ve been at this for hours, watched several videos, read tutorial blogs, and been experimenting with regex101. Anchors, lookahead/behinds, are confusing and I can’t seem to get anything to do what I want. Not looking to become an expert in writing regular expressions, but I would really like learning something new that can be directly applied to what I’m doing.


solution

I’m sure this can be simplified, but the following regular expression seems to work:

Example Here

/(\d{1,2}:\d{2}[ap]m\s–\s\d{1,2}:\d{2}[ap]m(?:.(?!\d{1,2}:\d{2}[ap]m))*)/g
var input = '1:19pm – 10:33pm ate tacos 10:35pm – 11:38pm 12:40am – 1:24am took a nap';
var matches = input.match(/(\d{1,2}:\d{2}[ap]m\s\s\d{1,2}:\d{2}[ap]m(?:.(?!\d{1,2}:\d{2}[ap]m))*)/g);

for (var i = 0; i < matches.length; i++) {
  snippet.log(matches[i]);
}
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Output:

1:19pm – 10:33pm ate tacos

10:35pm – 11:38pm

12:40am – 1:24am took a nap