I'm trying to parse the following line:
HSD| avg| 15 - 16 | 586784
in order to extract only the numerical values : 15, 16 and 586784
The following regexp works:
regexp {(^HSD\|)(\s*)(avg\|)(\s*)(\d*)(\s*-\s*)(\d*)(\s*\|\s*)(\d*)}
$line match sub1 sub2 sub3 sub4 sub5 sub6 sub7 sub8 sub9
puts "$sub5 $sub7 $sub9"
15 16 586784
I was wondering though if there would exist a simpler expression to
extract those numbers? My regexp doesn't look too optimized ;)
TIA.
--Bruno
Have you considered [scan] for this?
% set line {HSD| avg| 15 - 16 | 586784}
HSD| avg| 15 - 16 | 586784
% scan $line {%[^|]|%[^|]|%d - %d |%d}
HSD { avg} 15 16 586784
--
| Don Porter Mathematical and Computational Sciences Division |
| donald...@nist.gov Information Technology Laboratory |
| http://math.nist.gov/~DPorter/ NIST |
|______________________________________________________________________|
I didn't know scan!
scan $line "%s %s %d %s %d %s %d" string1 string2 val1 string3 val2
string4 val3
Works great ;) In fact simpler than regexp in such a case.
Thanks Donald.
set numList [regexp -all -inline {\d+}]
Bruce
I'd add word boundaries in case the words contain digits
set numList [regexp -all -inline {\m\d+\M}]
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry