Parse numerical values with space as delimiter while ignoring digit grouping (i.e. 1 020 vs 1020)

南笙酒味 提交于 2019-12-13 03:36:08

问题


Need some ideas here. I have a string that may look like "542.12 18.02 1 020.00 2.34" that I want to parse into seperate vars (with A_Space as delimiter). The problem is that the value 1020.00 is written on the form 1 020.00 so I get the vars 1 and 020.00. I can't come up with anything with RegExReplace cause as far as can understand from the documentation I have to replace the whole NeedleRegEx. Else I could have used " \d+( replaceonlythisspace)" as the "real" values always have a dot in it. I expect this could be done in some way within RegExReplace but I can't find something that says it does. I actually got it working now, but in a messy way:

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 := ""
}

Sure there must be a neater way doing the same thing?


回答1:


This will parse numbers that contain 1) zero or more embeded spaces and 2) a single decimal point:

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, " ")
}


来源:https://stackoverflow.com/questions/52376511/parse-numerical-values-with-space-as-delimiter-while-ignoring-digit-grouping-i

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!