I have an XML file.
<?xml version="1.0"?>
<catalog>
<book id="bk101">
</book>
<catalog>
I read the file and store it in file_data
set data [split $file_data "\n"]
foreach line $data {
regexp { book id=\"(.*)\" } $line all dummy
puts $all
puts $dummy
}
So here as you can see I am trying to read the book id and print it out.
I get the error dummy not found? Am I do it wrong?
Edit
Weirdly when I try this :
set mydata {<book id="bk101"> testing the code }
puts $mydata
regexp {book id="(.*)"} $mydata all part
puts $all
puts $part
Output
<book id="bk101"> testing the code
book id="bk101"
bk101
Have no idea the code at the top still shows error
The spaces in the RE are significant, and you place them around the original RE where there wouldn’t be any expected. If you want to parse XML though, it might be best to use tdom or TclXML.
You should check that the result of regexp returns a non-zero answer (meaning it found something), otherwise ‘dummy’ won’t get set, or will remain as was if previously set.