It's tricky to do this in a RegEx one-liner, I'm not sure that any of the attempts above cover all of the list below for example, here's an attempt I made:
Any ideas/comments are welcome.
Code: Select all
q::
;crop leading zeros (maintain minus sign, keep a 0 if required)
vList := "1000,-1000,000,-000,00.00,-00.00,0.0,-0.0,00123,-00123,00123.123,-00123.123,00.123,-00.123,2.00500,-2.00500,2.500,-2.500,2.000,-2.000,0002000.0002000,-0002000.0002000"
vOutput := ""
Loop, Parse, vList, % ","
{
vNum := A_LoopField
vOutput .= vNum "`t`t" RegExReplace(vNum, "^-?\K0+(?=[^0.]|0\.|0$)") "`r`n"
;vOutput .= vNum "`t`t" RegExReplace(vNum, "^-?\K0+") "`r`n" ;never keeps a leading zero
}
;Clipboard := vOutput
MsgBox, % vOutput
;crop trailing zeros after decimal point (remove decimal point if required)
vOutput := ""
Loop, Parse, vList, % ","
{
vNum := A_LoopField
vOutput .= vNum "`t`t" RegExReplace(vNum, "\.0+$|\..*?\K0+$") "`r`n"
}
;Clipboard := vOutput
MsgBox, % vOutput
;crop leading and trailing zeros
vOutput := ""
Loop, Parse, vList, % ","
{
vNum := A_LoopField
vOutput .= vNum "`t`t" RegExReplace(vNum, "^-?\K0+(?=[^0.]|0\.|0$)|\.0+$|\..*?\K0+$") "`r`n"
;vOutput .= vNum "`t`t" ZTRim(vNum) "`r`n"
}
;Clipboard := vOutput
MsgBox, % vOutput
return
;==================================================
;ZTrim() : Remove redundant leading/trailing zeroes from a number - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=33960
ZTrim( N := "" ) { ; SKAN / CD:01-Jul-2017 | LM:03-Jul-2017 | Topic: goo.gl/TgWDb5
Local V := StrSplit( N, ".", A_Space )
Local V0 := SubStr( V.1,1,1 ), V1 := Abs( V.1 ), V2 := RTrim( V.2, "0" )
Return ( V0 = "-" ? "-" : "" ) ( V1 = "" ? 0 : V1 ) ( V2 <> "" ? "." V2 : "" )
}