Getting Strings Before and After a Keyword

Hi All,

I'm trying to build a macro that will capture information before and after a specific keyword on a clipboard.

For example, if I copy to a clipboard the following text:

The rain in Spain falls mainly on the plain. If I designate the keyword "Spain", I'd like to return the result on either the clipboard or variable, "...rain in Spain falls mainly..." So, two words before and two words after the keyword. If anyone can help me with this type of filter, I'd greatly appreciate it!

Thanks for your time,
Scott

Hey Scott,

This is very dependent upon your use-case.

Will your object string always be a single line of text?

Or is that subject to change?

See the Search Clipboard action.

There's more than one way to do this of course, but here's an example:

Extract Words Before and After a Delimter String v1.00.kmmacros (6.7 KB)
Keyboard Maestro Export

-Chris

1 Like

How about this attempt?

image
Extract 2 Words Before and After Spain.kmmacros (2.9 KB)

Or this one:

image
Extract 2 Words Before and After Spain 2.kmmacros (2.9 KB)

1 Like

Hi Chris,

Yes, it will always be a single line of text. I'm going to give this a try today! Thanks for your input - greatly appreciated!

Scott

These all work flawlessly! As a follow up, I will be storing my keyword as a variable as it may change. In that case, can I simply reference the variable in the RegEx string? For example, instead of writing "Spain", I'll have %variable%keyword%.

One final question: let's say I get the result "rain in Spain falls mainly" But in a particular instance, the sentence ends at falls. So the result would be "rain in Spain falls. mainly". Is there a simple way to just limit the results right at the period, instead of including "mainly".

Yep!

So have you tried changing the last {2} to {1}?

image

I hope I'm understanding you correctly. Let us know.

KC

1 Like

I think I got everything working except the variable part. I now have:

([a-zA-Z]+\s+){2}%variable%keyword%(\s+[a-zA-Z]+){1}

...and am getting an error. I've tried just omitting the %variable% as well. What is the correct way to refer to a variable in the RegEx string?

Again, I appreciate the help!!
Scott

No mention is made of this (that I can see, anyway) in the KM wiki, but when referring to a KM variable, you need to do this:

%Variable%some_KMvar%

and NOT this:

%variable%some_KMvar%

In other words, use a capital V for variable.

Also, do not omit the %Variable% part in your regex.

2 Likes

Hey Scott,

Simple?...  :sunglasses:

Parsing text is an exacting business – especially when you're tying to accommodate different possible inputs.

In a case like this the smart thing to do is generally let go of the idea of writing the perfect regular expression and preprocess the text with a different regex.

Something like this which will remove all text from a period to the end of the string.

Find:

\..+$

Replace:

Nothing

You could change that to:

[.!?].+$

To accommodate more punctuation marks.

-Chris

1 Like

Great distinction! This worked! Thank you - I thought I was losing my mind trying to get this to work.

2 Likes

Got it! Works like a charm!

1 Like

Good deal!

A recent discussion on the use of regular expressions that might be worth a minute or two of your time.

These are my most relevant posts, but you might look over the whole thread.

RegEx question: how to search for white space repetitions (1 tab and 1 new line) - #8 by ccstone

RegEx question: how to search for white space repetitions (1 tab and 1 new line) - #15 by ccstone

-Chris