Let's assume I have a variable called 'time' whose value is '63 days 4 hours 28 minutes' and I want to insert a comma after every word without a preceding number. I wrote a small procedure to do the trick, but it is messy and rather cpu intensive, so I figured I'd use regular expressions instead. My problem is, despite hours of studying various regexp- manuals and tutorials, that I can't figure out how. This is how I want the string to look after formatting it: "63 days, 4 hours, 28 minutes"
The procedure, which simply scans through the string given, checks if the previous character was not a number and if the current character is an empty space, inserts a comma, looks like this:
Code: Select all
proc fixtime {time} {
set p ""
set r ""
foreach c [split $time ""] {
if {![string is integer $p] && [string equal " " $c]} {
append r ,
}
append r $c
set p $c
}
return $r
}
So, does anyone a little more experienced using regular expressions know how I could go on about this? If so, I'd appreciate it if you would share your knowledge with me. I've spent too many hours trying and failing to do anything else. Thanks in advance.