Subject: Regex Works on Regex101 but Capture Groups Empty in KM (v11.0.3) - No Match Failure Notification

Hi Peter and Keyboard Maestro Community,

I'm running into a puzzling issue with a "Search Variable Using Regular Expression" action in Keyboard Maestro 11.0.3 on macOS.

I'm trying to read lines from a plain text UTF-8 file (e.g., Ticker,CompanyName) and split each line into two variables (Local_Ticker and Local_Name) using the regex ^([^,]+),(.*)$.

The issue is that the regex ^([^,]+),(.*)$ works perfectly with my test data (e.g., NYSE:A,Agilent Technologies, Inc. - $A) when tested on regex101.com (ECMAScript flavor), correctly identifying Group 1 and Group 2.

However, in my Keyboard Maestro macro, while the "For Each" loop successfully reads the line into a Local Site variable (confirmed by a Display Text in Window action), the subsequent "Search Variable Local Site Using Regular Expression" action results in empty Local_Ticker and Local_Name variables.

Critically, even with "Failure Aborts Macro" OFF and "Notify on Failure" ON in the "Search Variable..." action, I do not receive any notification from Keyboard Maestro that the regex failed to match. The macro proceeds to the next action, but the capture group variables are empty.

What I've Confirmed/Tried:

  1. Basic Macro Execution:
  • The macro trigger works.
  • The Keyboard Maestro Engine is running.
  • The Macro Group is active and enabled ("Available in all applications" for testing).
  • A Beep action as the very first action in the macro works.
  • A Display Text "static text" in Window action as the second action (before the loop) works.
  • A Display Text "static text" Briefly action as the second action does NOT work (no window appears, though the beep before it works). This might be a separate minor issue or related.
  1. File Reading:
  • The For Each Local Site in The lines in: File "/path/to/my/file.txt" action successfully reads the first line into Local Site. This is confirmed by a Display Text "%Variable%Local Site%" in Window action immediately inside the loop, which shows the correct line content.
  • Initially, I had "Assertion Failed" errors in the Engine.log (CLinesInCollection.mm and LinesIn.mm). These errors seem to have been resolved by deleting the original input file and recreating it from scratch as a plain text, UTF-8 encoded file using TextEdit (Format > Make Plain Text, then save as UTF-8). The loop now proceeds to read the first line.
  • I have granted Keyboard Maestro Engine "Full Disk Access."
  • The input file currently contains simple, known-good data like NYSE:A,Agilent Technologies, Inc. - $A or even just Fff,ddd.
  1. Regex Action and Variable Display:
  • The regex pattern in KM is ^([^,]+),(.*)$ (typed carefully, no smart quotes/spaces).
  • "Search Variable Local Site Using Regular Expression" settings:
    • Search Local Site.
    • Regex ^([^,]+),(.*)$.
    • Save All to Local Site (or a test variable).
    • Save Group 1 to Local_Ticker.
    • Save Group 2 to Local_Name.
    • "Failure Aborts Macro": OFF.
    • "Notify on Failure": ON.
  • An immediately following Display Text in Window action shows Local Site correctly, but Local_Ticker and Local_Name are empty.
  • No "Regex failed to match" notification appears from KM.
  1. Data Cleaning:
  • I've added actions to Trim Whitespace variable Local Site from leading and trailing before the regex action.
  • I've also added Search and Replace Variable "Local Site" (searching for \r\n?|\n|\r and replacing with empty) before the regex action to remove any potential embedded newlines.
  • These cleaning steps did not change the outcome (capture groups remain empty, no failure notification).
  1. System:
  • Keyboard Maestro Version: 11.0.3 (from Engine.log)
  • macOS Version: [Please add your macOS version here, e.g., "Sonoma 14.4.1"]
  • I have restarted the Mac multiple times.

My main question is: Why would the regex capture groups be empty in Keyboard Maestro if the regex itself is valid for the data (as proven on regex101.com), the input variable Local Site appears correct, and Keyboard Maestro is not even reporting a match failure? It seems like the match might be succeeding internally in some way that doesn't throw an error, but the capture group assignment is failing.

Any insights or further diagnostic steps would be greatly appreciated. This has been quite a puzzle!

Thank you,

It's usually helpful to upload your macro, especially in cases like these, so we can see what you're working with and where the issue might be.

I'm still learning regex, but I've found certain expressions that aren't supported in KM; however, I don't think that's the problem here because your regex works fine for me. I suspect there might be something amiss in your For Each action, but I can't say for certain without seeing it.


1 Like

Loop Regex test.kmmacros (6.0 KB)

thank you so much for your kind advice. attaching the macro here.

not sure why mine is not working. looks very similar to urs


Mine is showing blank as the result..

It was a very sneaky error — you have a ⏎ at the end of your expression. I only discovered it when I double-clicked and noticed the selection took up the entire text field. Deleting that ⏎ should resolve the issue.

image

3 Likes

It is working now! Thank you so much! Really helpful. Been stuck with it for 5-6 hours before you helped to enlighten me. God bless you.

3 Likes

Not the problem here, but remember for the future that KM uses ICU Regular Expressions, so do your regex101 testing using the PCRE flavours.

Your (unchanged) macro does "Notify on Failure" for me, so check you're allowing KM Engine Notifications.

Nitpick -- the capture group variables are unchanged. They're empty because they were previously empty and the regex failed, not because the regex failed and so they were set to empty.

I mention it because that is forever catching me out when looping through items that may or may not match the regex. Here's an example of what happens, using your fixed macro but removing the , from the second line of sample text so it no longer matches:

Loop Failed Regex Example.kmmacros (6.0 KB)

Image

Giving:

Note the contents of the final "Display Text".

2 Likes

thank you for the advice!

I had wondered that too and didn't know what bessed matched ICU since it doesn't say that. You said flavours, so is either one is fine?

PCRE and PCRE2

image

ICU is based on PCRE, so I think those are the closest matches on regex101. The rest is guesswork and using "flavours" was more hedging my bets than a definitive statement! But I'd go with PCRE2 until proven otherwise.

1 Like

Note also that "works in regex101.com" even if you were able to use an ICU flavors, would still not necessarily get the same result, since the flags are different by default (for example, whether you are finding all or just the first match, and frequently the behaviour of the . and other such things).

2 Likes

Thank you, Peter for posting this information. Very helpful.