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.

Posted by technocrat2004 (technocrat2004), 17 February 2006
Hi friends,

I have two strings as below:
"IA - OSPF inter area route, E1 - OSPF external type 1” set to arg1
“[120/16]” set to arg2

when i try regexp to match arg2 to arg1 i am getting a 1 as return value. Can any one help me to trace what is happening.

Here you have the dump:.......

expect1.4> set line "IA - OSPF inter area route, E1 - OSPF external type 1"
IA - OSPF inter area route, E1 - OSPF external type 1
expect1.5> set arg "\[120/16]"
expect1.6> puts $arg
expect1.7> puts $line
IA - OSPF inter area route, E1 - OSPF external type 1
expect1.8> regexp $arg $line match
expect1.9> puts $match

Posted by Custard (Custard), 17 February 2006
I'm not a tcl regexp expert (at the moment, used to be, but more perl now) but it looks a little like the arg1  '[120/16]' is a character class, and probably matched the first occurrence of the digit '1' from the class..   (I need to look it up to verify this)

What is it you are trying to match?  Are you looking for a string '120/16' in the $line ?

And what would you like in $match after the regexp?


Posted by technocrat2004 (technocrat2004), 17 February 2006
Tahnk you for the reply bose........

I want to match "[120/16]" in the line. That is why i had set arg1 as set "\{120/16]". If I am wrong at any point please correct me as i am also new to regexp...

As far as I know in tcl we have everything as string.... so "[120/16]" should come as sting. And I am matching this string to $line. Also regexp should return if there is a full match of arg1 to arg2.....

Friends, please let me know if i am correct... if yes why I am getiing 1 as return value...... if no please expelain....

Yours Loving

Posted by admin (Graham Ellis), 18 February 2006
You're wanting to match a [ character literally as part of a regular expression using Tcl .... but the [ is a "special" twice over so it need double protection.   Let me explain.

Firstly, [ through ] mean "run this first" to the Tcl interpretter; to avoid this happening, you can wrap them in { through } or in " through " or protect them with a preceeeding \.

Secondly [ through ] in a regular expression means that you're looking for any 1 character from within the square brackets, and if you want to match literally you need an extra \ protection, remembering that the \ will also need protection from the Tcl interpretter.

Here are two solutions to illustrate this - the first uses { through } to defer the regular expression so that any \ characters in it do NOT ned further protection from the Tcl interpretter; the second uses " through " to group the regular expression and in this case the \ does need extra protection.

puts -nonewline "Please enter a line "
flush stdout
gets stdin myline

if {[regexp {\[12/160\]} $myline]} {
       puts matched
} else {
       puts missed

if {[regexp "\\\[12/160\\\]" $myline]} {
       puts matched
} else {
       puts missed

Posted by technocrat2004 (technocrat2004), 20 February 2006
Thank you Bose.................................
Thank you very much................................  
I got the point

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