Jump to content

mikell's thousands separator v2


pixelsearch
 Share

Recommended Posts

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

 

Link to comment
Share on other sites

43 minutes ago, pixelsearch said:

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

Thanks for sharing this RegEx ( @mikell as well :thumbsup:).

To make sure that the credits are fairly allocated, I have also given a 'Thanks' in the corresponding link.

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...