使用空格作为分隔符解析数值,同时忽略数字分组(即1 020对1020)

问题描述 投票:0回答:1

这里需要一些想法。我有一个字符串可能看起来像“542.12 18.02 1 020.00 2.34”,我想解析成单独的变量(用A_Space作为分隔符)。问题是值1020.00写在表格1 020.00上,所以我得到了1和020.00的变量。我无法想出任何与RegExReplace有关的原因,据我所知,我必须更换整个NeedleRegEx。否则我可以使用“\ d +(replaceonlythisspace)”,因为“真实”值总是有一个点。我希望这可以在RegExReplace中以某种方式完成,但我找不到它说它的东西。我实际上现在正在使用它,但是以一种混乱的方式:

nrofvaluesover1000 := 0
Loop , Parse , var, %A_Space%
{
if A_LoopField is Integer
{
    nrofvaluesover1000++
    thousands := A_LoopField
    continue
}
nr := A_Index - nrofvaluesover1000
If (nr = nrofthewantedvalue)
{
    if thousands is not space
    {
        MyValue := A_LoopField + 1000 * thousands
        thousands := ""
    }
    else
        MyValue := A_LoopField
    MsgBox '%MyValue%' 
    break
}
else
    thousands := ""
}

当然必须有一个更简洁的方式做同样的事情?

autohotkey
1个回答
1
投票

这将解析包含1)零个或多个嵌入空格和2)一个小数点的数字:

haystack := "1.1 200.2 3 000.3 400 000.4 5 000 000.5"
pos := 1

while (pos := RegExMatch(haystack, "[\d ]+\.\d+", match, pos)) {
  pos += strlen(match)
  MsgBox % strreplace(match, " ")
}
© www.soinside.com 2019 - 2024. All rights reserved.