Version 5 of Extract Numbers From a String

Updated 2022-10-02 18:23:30 by WJG

WJG 2022-10-01: A quick snippet on extracting a list of numbers from a string.

proc extractNumbers {str} {
        set res ""
        foreach c [split $str ""] {
                if { [string is integer $c] } {
                        set a 1
                        append res $c
                } elseif { $c eq "," || $c eq "." } {
                        if {$a} { append res $c }
                } else {
                        set a 0
                        append res " "
                }
        }
        return [string trim $res]
}

pyk 2022-10-02: What about numbers like "this .13 here?". Over at Regular Expression Examples there's a regexp command to extract numbers from a string.

wjg 22-10-22: Firstly, is ".13" a number? I'm not interested in numbers per se, but extracting sub-strings. The examples that I've seen and the examples that I actually understand are, respectively, many and none. I'm sure that there's some arcane regexp "formula" to extract a series of numeric substrings from a passage of text, it takes a better man than me to work that one out.