RogerW
December 16, 2021, 11:58am
1
I am trying to capture the two years from a data string that may be formatted in any of the following ways.
23 July 1914 - 11 July 2002
1914 - 2002
abt 1914 - abt 2002
2002
The year will always be a 4 digit number. The last year will always be there but the first one may or may not be present.
I would like to use Regex just so that I can learn Regex but if there is a better way using just KM that would help.
TIA
Roger
Lot of approaches – here's one.
If you combine regular expressions with some scripting, you can use a handful of simpler regex.
For example:
Line break [\r\n]+
Word break \s+
Four digits \d{4}
Here, capturing each year paired with a (one-based) line number.
Dates in numbered lines.kmmacros (5.4 KB)
Expand disclosure triangle to view JS Source
(() => {
"use strict";
const main = () => {
const rgxYear = /\d{4}/gu;
const
kme = Application("Keyboard Maestro Engine"),
sampleText = kme.getvariable("sampleText");
return JSON.stringify(
lines(sampleText).flatMap(
(lineString, iLine) => words(lineString)
.flatMap(
wordString => rgxYear.test(wordString) ? (
[
[1 + iLine, wordString]
]
) : [])
), null, 2
);
};
// --------------------- GENERIC ---------------------
// lines :: String -> [String]
const lines = s =>
// A list of strings derived from a single
// string delimited by newline and or CR.
0 < s.length ? (
s.split(/[\r\n]+/u)
) : [];
// words :: String -> [String]
const words = s =>
// List of space-delimited sub-strings.
s.split(/\s+/u);
return main();
})();
Sleepy
December 16, 2021, 4:40pm
3
How about:
cc's response below is very valid, and now that I've woken up, I see that my response misses one of your requirements about cases where there's only one variable. I could fix my response easily enough if you want me to. Here's the fix: (make sure you turn off error flags for the first Search macro.)
Bear in mind that something is incomplete about your request, because you didn't ask for me to indicate "how many results were returned." So I'm not sure how you can make this work for you.
ccstone
December 16, 2021, 5:11pm
4
Hey Roger,
You're not being clear about the desired output.
Your string has 7 substrings displaying a year.
You want to capture each one of those 7?
You want to place each one (even the duplicates) in a different variable?
Will there always be 7 dates, or will the macro need to dynamically create variables for a variable number of year strings?
-Chris
RogerW
December 16, 2021, 8:43pm
5
There can be only 2 returns. Your suggestion has worked great.
Thanks
Roger
Sleepy
December 16, 2021, 9:11pm
6
If you are happy, I'm happy.