[SOLVED] Regex: Using Partial "%triggerValue" Match

I'm trying to write a text-replacement regex that will take the typo "rpo" followed by any word character and replace the "rpo" portion with "pro" (and followed by the that arbitrary \w character). I tried this regex as the triggering string:

But that match includes the \w character and overwrites it, so "rpod" is replaced with "pro" rather than "prod".

Also tried a look-ahead:
(rpo)(?>[cd]) and then experimented with %TriggerValue% but couldn't find a way to just capture the look-ahead capture group.

By the way, I the above may not be a "look ahead" which I thought used an equals instead: (?=[cd]) but this didn't take any action.

You need to regex the trigger string to fire the macro and regex the trigger value to extract the character to use later. In this case that second regex only needs to capture the last character, so you can use .*(\w):

Replace rpo+ with pro+ .kmmacros (2.5 KB)

In fact, "Search text" regex can be .*(.) -- just the last character, since you did the "is a word char" check in the trigger -- and you don't even need a regex but can use a "Substring" action instead:

1 Like

It's good practice to use the explicit %Variable%<VariableName>% token in text fields to avoid accidents. From the manual:

You can also use a short form of just %Variable Name% to include variables as long as the variable exists and has a value and there is no corresponding text token, although generally it is better and clearer to use the longer form %Variable%Variable Name%.

Not a clue -- it does work in a KM regex Search and Replace though.

1 Like