Group timestamps with descriptions

I have data for tasks that were recorded using a schedule application. I am trying to parse breaks for each task.

An example of an interrupt line attached to a task might look like this:

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

I need to group this into timestamps with related descriptions. The above should be grouped as follows:

1:19 pm - 10:33 pm ate tacos

10:35 pm - 11:38 pm

12:40 - 1:24 in the morning took a nap

The description of the interrupt interval can contain basically any characters or be of any length. Some intervals have no descriptions.

I believe that regex will be the easiest way to get an array of intervals with their descriptions (if any).

So far I:

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

which corresponds to the timestamps 1:19pm – 10:33pm , 10:35pm – 11:38pm and 12:40am – 1:24am

I use JavaScript and match to parse this data. I want to make a regex that matches the timestamp and everything that follows it until the next timestamp.

I start with regex, so it's easy on me. I was in this for hours, watched several videos, read blogs for textbooks and experimented with regex101 . Anchors, looks / nape, are confusing, and I can not get anything to do what I want. Not wanting to become an expert in writing regular expressions, but I would really like to learn something new that can be directly applied to what I do.

+7
javascript regex timestamp
source share
3 answers

You can use the following regular expression:

 (\d{1,2}:\d{2}[ap]m\s*–\s*\d{1,2}:\d{2}[ap]m)(\D*(?:\d(?!\d?:\d{2}[ap]m\s)\D*)*) 

Watch the regex demo

The problem you are facing corresponds to text that does not match a specific pattern . This can be achieved either using a moderate greedy token or using the unroll-the-loop method. The latter is preferable since it requires less return. My regular expression is based on this technique.

Here is a regex explanation:

  • (\d{1,2}:\d{2}[ap]m\s*–\s*\d{1,2}:\d{2}[ap]m) - compares and fixes the time period of the group # 1 (I just added outer brackets and quantifiers * to \s classes) - since this is your regular expression, I won’t go into details
  • (\D*(?:\d(?!\d?:\d{2}[ap]m\s)\D*)*) is an expanded construction .*?(?=\d{1,2}:\d{2}[ap]m\s) , matching any pattern \d{1,2}:\d{2}[ap]m\s . He is placed in group number 2.
    • \D* - 0 or more characters except the digit
    • (?:\d(?!\d?:\d{2}[ap]m\s)\D*)* - 0 or more sequences ...
      • \d(?!\d?:\d{2}[ap]m\s) is a digit ( \d ) followed by 1 or 0 digits, followed by : and then 2 digits, then a or p , then m , and then whitespace
      • \D* - again, 0 or more characters except a digit.

JS demo:

 var re = /(\d{1,2}:\d{2}[ap]m\s*–\s*\d{1,2}:\d{2}[ap]m)(\D*(?:\d(?!\d?:\d{2}[ap]m\s)\D*)*)/ig; var str = '1:19pm – 10:33pm ate tacos 10:35pm – 11:38pm 12:40am – 1:24am took a nap'; var m; while ((m = re.exec(str)) !== null) { document.getElementById("r").innerHTML += "Period: " + m[1] + "<br/>"; document.getElementById("r").innerHTML += "Description: " + m[2] + "<br/><br/>"; } 
 <div id="r"/> 
+5
source share

I'm sure this can be simplified, but the following regex 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.imtqy.com/simple-snippets-console/snippet.js"></script> 

Output:

1:19 pm - 10:33 pm ate tacos

10:35 pm - 11:38 pm

12:40 - 1:24 in the morning took a nap

+4
source share

hope this helps:

https://regex101.com/r/dV7vY5/1

(\ d {1,2}: \ d {2} [ap] m) - (\ d {1,2}: \ d {2} [ap] m) ([\ s | az | AZ] +)

exit:

1:19 pm - 10:33 pm ate tacos

10:35 pm - 11:38 pm

12:40 - 1:24 in the morning took a nap

and you can complete each step:

  $1 - first hour (1:19pm) $2 - second hour (10:33pm) $3 - string ( ate tacos) 

example below:

 var string = '1:19pm – 10:33pm ate tacos 10:35pm – 11:38pm 12:40am – 1:24am took a nap'; var regex = /(\d{1,2}:\d{2}[ap]m) – (\d{1,2}:\d{2}[ap]m)([\s|az|AZ]+)/gi; var eachMatche = string.match(regex); for (var i = 0; i < eachMatche.length; i++) { snippet.log(eachMatche[i]); snippet.log('period : '+ eachMatche[i].replace(regex,'$1') +' - ' + eachMatche[i].replace(regex,'$2')); snippet.log('description : '+eachMatche[i].replace(regex,'$3')); } 
 <script src="http://tjcrowder.imtqy.com/simple-snippets-console/snippet.js"></script> 
+2
source share

All Articles