Quantcast
Channel: AutoIt v3 - General Help and Support
Viewing all articles
Browse latest Browse all 12506

RegExp - optimize --> ReduceSpaces()

$
0
0
I have got this simple function for reduction of more than 3 spaces to only two spaces
 
ConsoleWrite(ReduceSpaces(' abc def 1 2 ')& @CRLF) ConsoleWrite(ReduceSpaces('a b c d e')& @CRLF) ConsoleWrite(ReduceSpaces('abc')& @CRLF) ConsoleWrite(ReduceSpaces('')& @CRLF) ; 3 and more spaces reduces to 2 spaces Func ReduceSpaces($s)   Return StringRegExpReplace($s, "\x20{3,}", "  ") EndFunc
 
But I want also more general version of this function where as parameters will be number of spaces.
Here is my function but it's not optimal and I hope it can be done by some more clever RegExp without FOR/NEXT loop.
 
Please some RegExp guru help me increase my RegExp experiencies :-)
 
; N1 and more spaces reduces to N2 spaces Func ReduceSpaces($s, $n1=3, $n2=2)     $space2 = ''     For $i = 1 To $n2         $space2 &= ' '     Next     Return StringRegExpReplace($s, "\x20{"&$n1&",}", $space2) EndFunc

Viewing all articles
Browse latest Browse all 12506

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>