Macro: Keyboard Maestro fails to match (Regex)

Few days ago, I created two macros that would strip anything but the Twitter user handle in a url, set the the remainder in a variable, copy the value of this variable to the System clipboard.

The idea was to spare me all the clicks and contextual menus of a very simple workflow. All was working as expected as I was getting the results I wanted.

This morning only the second of these works. It seems the problem comes from the regular expression according to the notification I got from Keyboard Maestro.

I tested the regular expression in a regular expression validator and it works as I expected to as it selects "politicus" from the url "http://www.twitter.com/politicus/"

Url - Twitter handle extractor:
Display Text.kmactions (420 B)

I know the feeling – damned thing isn't working !! : - )

But it can be very helpful to cultivate a counter-intuitive habit of pivoting away from the pursuit of the guilty or broken, towards a healthy curiosity about what one has written.

It's more likely to be the regex that doesn't match all cases (or the macro that doesn't always get the url into the clipboard in time) rather than a mysterious malfunction in Keyboard Maestro – which simply evaluates exactly what we have written.

The real data usually has more variation than is anticipated by the simple model which we have defined in a regular expression,
and in a computing context regex is the name of a family of pattern-matching engines, rather than of a single fully-defined grammar, so the value of validators is variable.

It might be worth tracking down:

  • which cases it is working and failing with, (rather than focusing on the day on which it seems to be working or not.)
  • whether the url is actually making it into the clipboard (the regex may be returning a non-match on something else)

and it might also be worth testing with (or just using) a scriptlet (often a bit simpler than a regex to adjust and fine tune).

For example:

Scriplet for Regex-free JS action
Application('Keyboard Maestro Engine')
    .getvariable('sampleURL')
    .split('/')
    .reverse()
    .map(s => s.trim())
    .find(s => 0 < s.length);

Or if you want to stick with a regex, try this one:

https?://twitter.com/(.*[^/])/?

I've tried it on a few links and it seems to be reliable.

BTW: your kmactions file that you posted contains just this:
km 1 2020-11-02_11-27-39

which is not all that helpful :slightly_smiling_face:

1 Like

If I understand this correctly, the URL is the same, just selected in two different ways.
So what you really want is the LAST directory in the URL, provided it is a "twitter.com" URL.
If that is the case, then this RegEx should work, and provide the max flexibility of the other characters in the URL that you don't care about.

(?mi)http.+\.twitter.com.+?([^\/]+)\/?$

For details see: regex101: build, test, and debug regex

image