Regex Using a Variable as Replacement Including Capture Group

Yes, I get them to work on regex101.com and also RegExRX app.

Thank you @ccstone for your contribution, but in my case the numbered capture groups ($1, $2, etc.) don’t work either…

Personally, I have never found a need to use named capture groups so I do as you do.

I should add, by the way, that even using the KM-supported syntax that I posted previously, the replacement still fails which is why I asked whether you needed to use named groups. So your response of

was really the same thing I was trying. I guess we were both getting a bit desperate :scream:

1 Like

Neither one of them support ICU RegEx...

OK, but here’s the test I did regex101: build, test, and debug regex using the ICU syntax I posted previously.

I assumed since it worked under Pcre and was also ICU-correct then it should work in KM. Silly me :person_shrugging:?

I'm guessing you're going one level of indirection too far -- by the time the "replace" variable is expanded it's treating the $1 as literal text. Compare it to removing the $1s from your list text and then using $1%Variable%LocalmyReplace% as the replace -- works fine when there is a capture group but fails and uses "$1" as a literal when there isn't.

1 Like

:rofl:

Hi Chris,
I thought I'd do a test of named capture groups in KM. Here's the test macro:

Download Macro(s): Test Search Replace with Named Groups.kmmacros (3.6 KB)

Macro-Image

Keyboard Maestro Export

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

Running it gives the result:

Search Pattern:	(?<name>[Qq])ui
Replace Patten:	${name}ua
Input:			The quick brown fox
Output:			The quack brown fox

so it does seem to work OK.

2 Likes

Sure. Because no further processing happens after the token expansion that expands the replacement variable. So just like if the replacement text was %LongDate%, your result would be %LongDate% not the date, the $1 has no meaning within the variable.

3 Likes

Hi @Augustin - so from what @peternlewis (thanks!) is saying, it is not possible to achieve what you want. My old brain cells are having a hard time getting to grips with the explanation but regardless - the fact is it does not work :man_shrugging:

I think it is @tiffle, by riffing off your original idea. A single replacement pattern should be quite simple...

Go through each off the search/replace pairs, splitting them out. If the replace variable contains replacement patterns, pseudo-array on those and build your replace text field using eg %Variable%Local_replace[1]$1%$1%Variable%Local_replace[2]$1% But if the replace variable doesn't contain a $1 use the variable as usual.

A multiply-occurring single replacement pattern or multiple patterns will be more difficult and will probably need a different approach...

2 Likes

Not a bad idea @Nige_S but I've just worked out a different way of doing it using (sigh) AppleScript which I'll post in a minute.

Well @Augustin, the way we were trying to do this doesn't work, but there is another way that does seem to work.

The solution I've come up with relies on 2 components:

  1. Your original macro (more or less) with the Search and Replace with Regex action replace by a call to a subroutine macro.
  2. An additional macro in the form of a subroutine that perform the regex search and replace.

Here is the main macro:

Download Macro(s): Mise en forme | Anciens | Substantifs & adjectifs TEST v6.kmmacros (5.8 KB)

Macro-Image

Keyboard Maestro Export

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

and here is the subroutine:

Download Macro(s): [SUB] Regex Search Replace with Variables and AppleScript.kmmacros (7.7 KB)

Macro-Image

Keyboard Maestro Export

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

You need to install both macros for it to work.

This is the output when run:

bâtir
bêtes
Chrétiens
ecriture

Note: you must be absolutely sure about your regex search and replace patterns because obviously if they're wrong you'll get problem!

EDIT:

I've tidied up the subroutine and added some usage notes to it.

1 Like

Nice! Here's my stab at it -- not as fully featured as yours though:

AS Regex.kmmacros (4.5 KB)

Image

2 Likes

Great. Thanks for testing!

It's vitally important to test the base code and make sure it works before getting bogged down in the advanced parts of a macro.

I should have done specifically this myself, but I got caught assuming... I assumed that since CotEditor supported ICU RegEx it should support the same ICU RegEx Keyboard Maestro does.

Apparently not – CotEditor does not support this syntax on Mojave – and neither do Drafts or Script Debugger 8.x (which bothers me).

If someone can conveniently test this RegEx in CotEditor on a more advanced system than Mojave I'd appreciate it.

Search Pattern:

(?<name>[Qq])ui

Replace Patten:

${name}ua

Test Text:

The quick brown fox
1 Like

That’s nice @Nige_S :sunglasses:

Thank you so much @tiffle and @Nige_S ! You've provided a tremendous solution to my problem and I'm very grateful for your help! It's a nice thorn in my side you're taking away.

1 Like

Indeed; I like it...

The one thing I'd specifically change in the overall macro is how the regex is initially displayed – I'd change that into a table for better reading comprehension.

** I have KM's variable font sent to Menlo Regular 14, so I'm less likely to be caught-out by odd characters.


Download Macro(s): AS Regex.kmmacros (4.8 KB)

Macro-Image

Keyboard Maestro Export

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

2 Likes

FYI I thought I’d dig a little deeper @ccstone and followed a link on regex101 about someone who requested that ICU flavour be added. The link is ICU flavor · Issue #1414 · firasdib/Regex101 · GitHub.

Apparently the only difference between PCRE and ICU is that ICU does not include the \K metacharacter which “resets the starting point of the reported match. Any previously consumed characters are no longer included in the final match”.

Because the difference is so small, the ICU request was not taken any further.

I have to say I’m not that motivated to compare ICU and PCRE line for line so I’m just accepting what I read in that linked page meaning as long as I don’t use \K I can use the PCRE flavour for testing.

Hey Taj,

Thanks for researching. :sunglasses:

My experience has been that there are other differences, but it's been years since I actually researched – so what you discovered could well be true now.

-Chris

1 Like