You're still confused about the 'order of operations' in this situation.mikeyww wrote: ↑25 Feb 2023, 08:25In my view, the following sum is 26, and so this is the meaning of adding the orange to the apple-- in some situations.
This would replace the entire match with three plus the backreference's value.Code: Select all
#Requires AutoHotkey v2.0 num := RegExReplace("number23", ".*?(\d+)", "$1") MsgBox 3 + num MsgBox 3 + "23" num := RegExReplace("number23", ".*?(\d+)", 3 + "$1")
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.
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)
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.