My apologies if this question has already been asked and solved, but I haven't had any luck with a search of the forum.
For my teaching and my research, I use a macro to try to change texts written in Middle French into modern spelling.
For this, I have a (long) list of Search & Replace statements using regexes placed one after the other in a text file. They are listed following the same pattern for each line: mySearch/myReplace.
The macro gets the contents of each line, declares the variable mySearch, declares the variable myReplace, and performs a replacement by calling these two variables.
My problem is that it doesn’t work when there are groups captured in the myReplace variable. (It’s not the same behavior when I number ($1) or name (${name}) the groups, but it doesn’t work in both cases.
So, is it possible to use the captured groups in a variable that will be called as a replacement in a search and replace command ?
I guess there are conflict issues that I am not aware of.
I've looked at your macro and can offer 2 comments:
The method you use to extract the search and replace patterns from your mySearchReplaceList is overly complicated and can be simplified considerably by using KM Arrays with a custom delimiter. I've done this for you in the new version of your macro below.
I've tested your last search pattern ([dl][’'])escri(?![m]) in regex101.com which shows there is something wrong with it. Unfortunately I have to leave right now so I can't offer a solution but maybe someone else will be able to.
Oh, thanks so much to both of you for your far more elegant suggestions than mine! I'm just starting out with Keyboard Maestro, and I'm still trying to get the hang of all the possibilities it offers.
I have tried both of your suggestions, but each time it does not solve my first issue.
The replacement capture groups ($1, $2, etc.) do not work any more when they are placed in a variable. But maybe it just can't be done this way...
Thanks again for your time and effort in answering my questions!
Ok @Augustin I have the solution for you and it is based on the contents of this discussion in the KM forum:
I'll leave it to you to read and digest that discussion so you'll understand what's going on but the modifications to your macro consist of the two red-coloured actions in this new version:
Thank you very much @tiffle for taking the time to help me!
At first I thought that your macro solved my problem, but in fact it doesn't: in your macro none of my regex with special characters work anymore (whereas before it was only those with capturing groups).
If I understand correctly, the two actions you added disable all the special characters in my regex. But, I need precisely the opposite: I need them to be active and especially the capturing groups in the replacement regex ($1, $2, etc.), which is not the case at the moment.
Looking closely at what you've provided I see that the third search/replace pattern is incorrect (as I said in my original reply to you). It should read:
Again @tiffle, many thanks for your investigation on this!
Just to answer that: I have no real reason to use a named capture group here, I was just trying another syntax to see if I had better luck with it. But in my use of regular expressions, I use exclusively the numbered capture groups ($1, $2, etc.).
At last, the result you get is indeed the result I struggle with! You have perfectly summed up my issue (and greatly simplified what I had unnecessarily complicated).
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
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.
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.
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
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...