Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Pattern seraching/matching

Posted by smallsteps (smallsteps), 21 September 2007
Hi There,

I am facing some problems in TCL scripting:

I need to capture a pattern in the a string like

"<?xml version="1.0" encoding="UTF-8" ?><CommandList><Response><Interface>cs2kCfgMgrIf</Interface><Methods><assocMG usn="1" version=" 1.0"><ReturnData><RC>38</RC><MsgTxt>The MG is already provisionedThe Gateway name is already defined on GWC-7</MsgTxt></ReturnData></assocMG></Methods></Response></CommandList>"

Ideally I would like to capture the Return data as it an error code from the command thats sent and if the return code is not 0 then it should break. So in this case; <RC>38</RC> and so on. But my script doesn't do that. My script is as below:

if {[string match "<RC>0</RC>" "$response"] == 0 } {
        #send a message back that it was okay
       } else {
              #send a message back that it failed
               # exit the script.

Could you please tell me what am I doing wrong here.

Thanks in advance,

Posted by admin (Graham Ellis), 21 September 2007
Firstly, "break" breaks you out of a loop ... but you have no loop in the sample code supplied.  I believe that it's redundant and the exit is all you need to leave the script.

If you want to capture the error string, then I would advise you to do so using a regexp command along the lines of
regexp <ReturnData>.*</ReturnData> $response target
which should capture the return data part of your string into a variable called target.  Your current code doesn't identify the "interesting bit" of the incoming string in any way ... it makes no attempt to extract it ... and that's why your program doesn't make any specific use of it.

Posted by smallsteps (smallsteps), 21 September 2007
Thanks Graham. Got the mistake. Would try it out.

Posted by smallsteps (smallsteps), 21 September 2007
Hi Graham,

It worked thanks. Another basic question is that I need to read values only in between the tags.

<RC>0</RC> , Here only value 0 is interesting for me.
or <RC>1401</RC>, in this case only 1401 is interesting to me.

There are several tags like this where I need to check for the values inside the tags. Now I can read the complete tags.

Thanks in advance.

Posted by admin (Graham Ellis), 21 September 2007
You'll need to use the submatch vars - have a look at the regexp manual at

Basically, you put round brackets around the interesting bits that you want to capture, and then suppl an extra variable name on the end on which that intersting bit is to be stored

Posted by smallsteps (smallsteps), 24 September 2007
Hi Graham,

Somehow am stuck.

set response { <RC>0</RC> }
puts "response $response"
regexp {>(.*)</}  $response target
puts "target is $target"


response  <RC>0</RC>
target is >0</

I want to get rid of the characters around 0 . Could you please advice?

Posted by admin (Graham Ellis), 24 September 2007

regexp {>(.*)</}  $response target


regexp {>(.*)</}  $response everything target

The first extra variable contains the entire match, and subsequent ones the parts of the incoming string that matched each of the regular expression "interesting bits" it turn.

Posted by smallsteps (smallsteps), 26 September 2007

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho