Training, Open Source computer languages

This is page http://www.wellho.net/forum/The-Tcl- ... guage/RegEXP.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
RegEXP

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]"
[120/16]
expect1.6> puts $arg
[120/16]
expect1.7> puts $line
IA - OSPF inter area route, E1 - OSPF external type 1
expect1.8> regexp $arg $line match
1
expect1.9> puts $match
1


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?

Bruce

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
Technocrat......



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.

Code:
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 www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

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