Subject: parsing html attribute strings
From: Glenn Jackman
Date: 11/23/2007 7:06:20 PM
I'm using htmlparse. Great package, but it seems to be missing one
feature. One of the arguments passed to the callback command is the raw
tag attribute string, for example:
src='foo' title="bar baz" alt=***
There is no proc in htmlparse that will parse that into name,value
pairs.
The best I've come up with is:
proc parse_atts {attr_string} {
set re {(\w+?)\s*=\s*(.+?)\s*(?=$|\w+?\s*=)}
set result [list]
foreach {- name value} [regexp -all -inline $re $attr_string] {
# unquote the value
if {[string match {'*'} $value] || [string match {"*"} $value]} {
set value [string range $value 1 end-1]
}
lappend result $name $value
}
return $result
}
Is there a better way? Am I re-inventing a wheel?
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
|