0
votes

I need assistance with RegExReplace in AHK.

I want my script to search text and keep only numbers after $ sign.

Right now I have:

F1::

 Clipboard = 

 SendInput, ^c

ClipWait

Variable := Clipboard

NewVar :=   RegExReplace(Variable,"[^.0-9]+", "{+}")

send % NewVar

return

For example if text is "unit $400 unit 500 $400" I get 400+500+400+

What I would like to get is "400+400+"

I havent been able to figure out how to exclude numbers that dont have $ and I am not sure RegEx is the best thing to use here.

Any help would be great!

2
Why not extract all those matches? \$(\d+(?:\.\d+)?) would suffice. Pos := 1, => While Pos { Pos:=RegExMatch( str, "\$(\d+(?:\.\d+)?)", M, Pos+StrLen(M1) ) Match%A_Index% := M1 } - Wiktor Stribiżew
@Wiktor Stribiżew As you probably realized I am new to this. I have a problem with using this code, and keep getting 0 as a result. If you could please post completed code or some example so I can figure out what Im doing wrong. - Roki_09

2 Answers

0
votes

I don't know how to do this with RegExReplace, but I can suggest an example using the RegExMatch function.
Unfortunately, AutoHotkey doesn't support the /g/ flag for the global searching, so you have to use a loop to search all values you need.

#NoEnv
SendMode Input

F1::
    Clipboard = 
    SendInput, ^c
    ClipWait
    i := 1
    while pos := RegExMatch(Clipboard, "\$(\d+)", match, i)
    {
        i += pos
        sendinput % match1 "{+}"
    }
    ; sendinput {backspace} ; uncomment this line if you want the last PLUS character to be removed
return
0
votes

This is the final version, that does what I need. Thank you all for advise!!!

F1::        
Clipboard = 
 SendInput, ^c
ClipWait
Pos := 1
While Pos {
    Pos:=RegExMatch( Clipboard, "\$(\d+(?:\.\d+)?)", M, Pos+StrLen(M1) )
    Match%A_Index% := M1 
    M2  := M2 Match%A_Index% "{+}"
}
Send % "=sum(" M2 
Send    {BS}{BS}){enter}
M2 :=
return