How Do I Extract Dates From Multi-Line Text?

Need an update for the workflow. My previous example provided lines with the dates in them. How can I go about extorting the date if there is a bunch of text between the lines?

1/10 - May 14, 2019

Bunch of text. Bunch of text
Bunch of text
Bunch of text
Bunch of text
Bunch of text

2/10 - May 24, 2019

Bunch of text. Bunch of text. Bunch of text.
Bunch of text

3/10 - June 3, 2019

When I check the RegEx pattern at regexr.com, everything works as expected.

When I use KM, I get a RegEx error when I try to execute the workflow. Any idea why?

Session Dates.kmmacros (11 KB)

Your new workflow is significantly different, and thus should be posted as a new topic. I have moved it for you this time.

This requires a different approach, to first extract ONLY the lines that contain a date, and then extract the date from that line.

The same RegEx can be used in both the For Each and the RegEx Search:
(?m)\d+\/\d+\h+\-\h+(\w+.+?)$

for details, see https://regex101.com/r/jtzbHY/1/

Here's a simple macro that does ONLY the extraction. You will need to use/adapt to your specific workflow:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example Results

image

MACRO:   Extract Dates from Multi-Line Text [Example]

**Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+**
(Macro was written & tested using KM 9d9 on macOS 10.14.5 (Mojave))

#### DOWNLOAD Macro File:
<a class="attachment" href="/uploads/default/original/3X/4/6/46526ce501b4d6353915bebeb1c2f424ae477802.kmmacros">Extract Dates from Multi-Line Text [Example].kmmacros</a>
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

![image|508x1228](upload://7ZPJxXJ4rd7uVdUFMBzaFjJ1ooR.png) 

[details=Full-Sized-Image]
<img src="/uploads/default/original/3X/3/a/3aed78cd98a72747b857c0b92fc8946fc0b16cf5.png" width="508" height="1228">
[/details]

Hey @slyfox,

There's nothing wrong with using regular expressions for this sort of job, but it's generally easier and more flexible to use Apple Data Detectors.

Here's the output:

image
I can change the output format to anything you want with the addition of a bit more code.

-Chris


Extract Dates from Text Using Apple Data Detectors v1.00.kmmacros (6.8 KB)
      ⇢ Created with KM v9.0.
image

Hey Chris, one issue that might occur with Data Detectors looking at all text in the source is if some of the "bunch of text" contained text that could be construed as a date, like say "8/13", or even an actual date when then only dates you want are in the "header rows" as defined by the RegEx.

Yep, that's another thing you'd have to do.
May I suggest that you allow for setting of date format in KM variable, using the ICU format, and then if that variable is empty/missing, just output in a standard format (I'd suggest the ISO format).

Having said all that, I really like the ASObjC Data Detectors, and use it often.

@JMichaelTX Thank you for your solution.

Any idea why I am getting a different result from your picture? I get this:

%Local__DateList%Local__Date%
May 24, 2019
June 3, 2019

I figured out the solution to my original problem of extracting dates from text and pasting them into a numbers column.

Thank you !

Keyboard Maestro 8.2.4 “Extract Dates from Multi-Line Text [Example]” Macro

Extract Dates from Multi-Line Text [Example].kmmacros (4.7 KB)

Looks like you have NOT entered the KM Variable Tokens correctly.
You left out a % between the variables.

They should be:
%Local__DateList%%Local__Date%