RegExReplace backreferences cannot be manipulated

Share your ideas as to how the documentation can be improved.
Saiapatsu
Posts: 17
Joined: 11 Jul 2019, 15:02

Re: RegExReplace backreferences cannot be manipulated

Post by Saiapatsu » 26 Feb 2023, 09:08

mikeyww wrote:
25 Feb 2023, 08:25
In my view, the following sum is 26, and so this is the meaning of adding the orange to the apple-- in some situations.

Code: Select all

#Requires AutoHotkey v2.0
num := RegExReplace("number23", ".*?(\d+)", "$1")
MsgBox 3 + num
MsgBox 3 + "23"
num := RegExReplace("number23", ".*?(\d+)", 3 + "$1")
This would replace the entire match with three plus the backreference's value.

Many users are not knowledgeable about how AHK adds numbers, or how AHK differs from JavaScript. They may not have a deep understanding of how functions actually work.

At least for me, a regex tutorial is not what I am suggesting or seeking here. My observation is merely that the issue confuses some readers and users of AHK. If you agree that the documentation should say more, then you can recommend an alternative to my suggested text, unless you agree with it.
You're still confused about the 'order of operations' in this situation.

It appears that you think 3 + $1 is evaluated *after* the RegExReplace runs, yielding 26.
In reality, 3 + "$1" runs before RegExReplace is called (because you need to know all of the arguments to call a function) and throws an error because adding 3 to a non-numberlike string is a nonsense operation.

What you're actually asking for is a function/procedure as Replacement.
AHK doesn't have this feature, but you've reminded me that it's REALLY hurting for it.

If AHK could do that, then here's what your code would look like:

Code: Select all

#Requires AutoHotkey v2.0
Add3ToMatch(MatchInfo)
{
	return 3 + MatchInfo[1]
}
num := RegExReplace("number23", ".*?(\d+)", Add3ToMatch)
Now Add3ToMatch is an expression that evaluates to that function named Add3ToMatch. This happens before RegExReplace is executed.
RegExReplace needs to call this function for every match with a match object (same as the one that RegExMatch creates) and replace its match with whatever this function call returns.

This means RegExReplace is no longer replacing "number23" with 3 + "$1", which is utter nonsense.
It is now replacing it with the intent to add 3 to the first subpattern of whatever it matched. This is exactly what you asked for: adding 3 to the first subpattern.
It can check whether Replacement is such an "intent" instead of a string and carry out said intent after finding the match, not before it is even called.

So ultimately this feature request is to allow RegExReplace to take a function as an argument.

User avatar
mikeyww
Posts: 27216
Joined: 09 Sep 2014, 18:38

Re: RegExReplace backreferences cannot be manipulated

Post by mikeyww » 26 Feb 2023, 09:38

Thank you for the details. Your explanation makes sense except that "$1" by itself would not seem to be evaluated before the function call, so the extrapolation to how 3 + "$1" is handled may not be apparent to the unfamiliar coder. My original post also demonstrates the potential confusion because string concatenation in that function parameter is valid.

I'm not confused about anything. I am pointing out that some readers and users of AHK are confused about this, so it warrants clarification in the documentation.

I appreciate the feature request, and support it. There is a separate (parent) board for wish-list items other than documentation.

My suggestion for the documentation stands.

Backreferences cannot be used as operands or function parameters.

Post Reply

Return to “Suggestions on Documentation Improvements”