Hi everybody
mikell kindly shared with me his 2nd regex version for adding thousands separators. This 2nd version takes care of negative numbers too.
I told him that his regex was important for the AutoIt community and it should really find its place in the Forum.
So with his permission, I post his code & comments concernant the \G anchor. All credits are his, when he started with version 1 in this link
; mikell's thousands separator version 2 (november 2020)
$sInput = "-10024000.22345"
$sOutput = StringRegExpReplace($sInput, '\G([+-]?\d+?)(?=(\d{3})+(\D|$))', '$1,')
MsgBox(0, "mikell wiz \G - Result before and after", $sInput & @CRLF & $sOutput)
#cs
How \G works :
\G is an anchor which matches at the beginning of the subject string or at the end of the previous match. In this case it is used as a "forced failure" tool :
1. first \G matches at the beginning of the string
2. then the regex searches for ("+" or "-" (optional) and one or more digits) followed by (one or more 'packs' of 3 digits and a non-digit or the end of string). "-10" matches
3. \G matches right after "-10", the regex restarts searching from this position in the same way than 2. and "024" is found
4. \G matches right after "024", the regex keeps on searching but the condition in 2. is not fulfilled any more, so the regex fails and the final result is returned
#ce