Regex help. exclude words that contain the letter 'a'

Regex help. exclude words that contain the letter 'a'

title is hopefully, self explanatory

For Each.kmactions (71 KB)

Keyboard Maestro Export

Your approach looks very, very inefficient. You are adding words one line at a time in a loop that goes through each line in a dictionary one at a time. That might be very slow. Here's how I would do it.


That will take one input file and create another one that doesn't contain words with the letter "a" in it.

Thanks for the quick reply. I am always interested in speeding up my macros. I am still interested in why this Regex does not work in KM as it does on Regex101 website.

Your regular expression is only looking at the first line of the text block. To make it a multi-line expression, prepend (?m) so that your expression reads (?m)^(?!.*a).*.

See the wiki page on Regular Expressions in Keyboard Maestro for more details.

1 Like

Here's a shell script approach:

REMOVE Lines Containing String.kmmacros (21 KB)

Macro screenshot

1 Like

thanks. I guess what tripped me up was I had used this action with a different regex that worked without the multiline expression. see attached. not sure I understand why it was not required in this similar action. also the speed of this action is similar to using grep. the first "For Each" action posted was very slow compared to using grep. any comments are appreciated.

For Each.kmactions (72.3 KB)

^(?!.*a).* starts with the caret character ^, which matches the beginning of a line. You very likely want this (along with (?m)) so that each line/word is treated separately.

Without the ^, your expression effectively treats the entire blob of text as a single line.

s[bcf..][bcf..][bcf..][bcf..] also sees one big blob, but will only seem to match individual lines because you're explicitly listing the possible characters, which do not include newline characters.

The .* in your (?!.*a).* would match newlines, so running it without the ^ would return mostly noise.

Got it ! but speed is now slower. at my age (73), looks like I will be learning grep commands. haha. I was using regex hoping to get the speed I see on the regex101 website. Many thanks everyone.