Sion C
Sion C

Reputation: 451

Regexp - find a value shown after string, (TCL)

I want to return a value to $output, from out_buffer, so i did :

set output ""
set out_buffer {Unevictable:           0 kB}
#regexp -line {Unevictable:.* (.*\d).*KB} $out_buffer dummy output
if {!($output == "0")} {
    return 0
} else {
    puts "Unevictable is OK (equal 0)"
}

It works fine, but if out_buffer is like:

set out_buffer {cat /proc/meminfo | grep Unevictable
Unevictable:           0 kB
root@ltqcpe:/ramdisk/tmp# }

the return is null. What can I do ? that in any combination the value after Unevictable: will be put into $output.

Upvotes: 0

Views: 262

Answers (2)

Donal Fellows
Donal Fellows

Reputation: 137577

You probably want to use the -line option to regexp so that ^ and $ are line-aware. (Maybe the -nocase option too.) Then you can do this (which I've tested with both your sample input strings):

regexp -line -nocase {^Unevictable:\s*(\d+)\s*kB$} $out_buffer -> size

Also remember to check the result of regexp; it's the number of times the RE matched, which is 0 or 1 (conveniently boolean!) unless you also pass in the -all option.

Upvotes: 0

Dinesh
Dinesh

Reputation: 16428

There can be many ways to write regular expressions to match your string. Try something like

if {regexp {Unevictable:\s+(\d+)\s+kB} $out_buffer ignore size } {
    puts "size = $size"
}

Upvotes: 0

Related Questions