'Updating my current script which modifies multiple lines into a single one

My current script copies text like this with a shortcut:

:WiltedFlower: aetheryxflower ─ 4
:Alcohol: alcohol ─ 3,709
:Ant: ant ─ 11,924
:Apple: apple ─ 15
:ArmpitHair: armpithair ─ 2

and pastes it modified into a single line

Pls trade 4 aetheryxflower 3 alcohol 11 ant 15 apple 2 armpithair <@id>

As you can see there are already two little problems, the first one is that it copies only the number/s before a comma if one existed instead of ignoring it. The second is that I always need to also copy before hitting the hotkey and start re/start the script, I've thought of modifying the script so that it uses the already selected text instead of the copied one so that I can bind it with a single hotkey.

That is my current script, it would be cool if anyone can also tell me what they used and why exactly, so that I also get better with ahk

!q::
list =
While pos := RegExMatch(Clipboard, "(\w*) ─ (\d*)", m, pos ? pos + StrLen(m) : 1)
 list .= m2 " " m1 " "
Clipboard := "", Clipboard := "Pls trade " list " <@951737931159187457>"
ClipWait, 0
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the clipboard.
return


Solution 1:[1]

If the pattern of your copied text dont change, you can use something like this:

#Persistent

OnClipboardChange:

list =
a := StrSplit(StrReplace(Clipboard, "`r"), "`n")
Loop,% a.Count() {
    b := StrSplit( a[A_Index], ": " )
    c := StrSplit( b[2], " - " )
    list .= Trim( c[2] ) " " Trim( c[1] ) " "
}
Clipboard := "Pls trade " list " <@951737931159187457>"]
ToolTip % Clipboard   ;   just for debug

return

With your example text, the output will be:

Pls trade aetheryxflower ? 4 alcohol ? 3,709 ant ? 11,924 apple ? 15 armpithair ? 2 <@951737931159187457>

And this will run EVERY TIME your clipboard changes, to avoid this, you can add at the top of the script #IfWinActive, WinTitle or #IfWinExist, WinTitle depending of your need.

Solution 2:[2]

The answer given would solve the problem, assuming that it never changes pattern as Diesson mentions.

I did the explanation of the code you provided with comments in the code below:

!q::
list = ; initalize a blank variable
 ; regexMatch(Haystack, regexNeedle, OutputVar, startPos)
 ; just for frame of reference in explanation of regexMatch

While ; loop while 'pos' <> 0 
pos := RegExMatch(Clipboard ; Haystack is the string to be searched, 
in this case the Clipboard

    , "(\w*) ? (\d*)" ; regex needle in this case "capture word characters
(a-z OR A-Z OR 0-9 OR _) any number of times, space dash space
then capture any number of digits (0-9)"

    , m ; output var array base name, ie first capture will be in m1
second m2 and so on.

    , pos ? pos + StrLen(m) : 1) ; starting position for search
 "? :"used in this way is called a ternary operator, what is saying
is "if pos<>0 then length of string+pos is start position, otherwise
start at 1". Based on the docs, this shouldn't actually work well
since 'm' in this case should be left blank

 list .= m2 " " m1 " " ; append the results to the 'list' variable
followed with a space

Clipboard := "" ; clear the clipboard.

Clipboard := "Pls trade " list " <@951737931159187457>"

ClipWait, 0 ; wait zero seconds for the clipboard to change

If ErrorLevel ; if waiting zero seconds for the clipboard to change
doesn't work, give error msg to user.
 MsgBox, 48, Error, An error occurred while waiting for the clipboard.
return

Frankly this code is what I would call quick and dirty, and seems unlikely to work well all the time.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dieisson Silva dos Santos
Solution 2