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.
Simple question (sorry)

Posted by eliasson (eliasson), 29 April 2008

I'm having a problem (newbie again) with regsub (and regexes in general, beginning to hate them).  I do the following:

# Clean up output from utility.
  set lineList [split $expect_out(0,string) \x1b]
  foreach j $lineList {
     regsub {\x3b} $j "" j
     regsub {[0-9]+} $j "" j
     regsub {\x5b} $j "" j
     regsub {[G-L]} $j "" j
     puts $:utfile $j

and this works just fine.  But I would like to combine the regsubs into one line & I think that's possible?  However, every grouping I've tried with []s, ()s, {}s, on a single line hasn't worked for me.  What should this line look like?


Posted by eliasson (eliasson), 2 May 2008
Nevermind, I've got it down to 2 lines.  To clean up this buffer:
getdark 7 getdarkondark   :  Get the current on/off state of the Reflective Memory board's  Dark On Dark featureUsage:  getdarkondark         

I split into lines based on the 'esc's:
  set lineList [split $workBuff \x1b]

Then clean each line as follows:
  foreach j $lineList {
     regsub "\[\[0-9]+;\[0-9]+\[H-L]+" $j "" j
     regsub "\[\[H-L]+" $j "" j

The second 'regsub' line is needed to remove '[K', I'm not sure why.  But this works for now.

Posted by admin (Graham Ellis), 3 May 2008
on 05/02/08 at 20:54:35, eliasson wrote:
The second 'regsub' line is needed to remove '[K', I'm not sure why.  But this works for now.

Probably because regsub only works on the first match in a line by default.   Try the -all option.

Posted by eliasson (eliasson), 5 May 2008
Well, I appreciate the assist, but no, -all didn't help.  I guess it must be something to do with the telnet buffer layout, but I don't know what.  I'm also having probs searching for () as literals.  I expect \( to 'literalize' :^) the (, but I keep getting a failure of unbalanced ()s.  So, I keep looking for ways to deal ...


Posted by admin (Graham Ellis), 7 May 2008
Hi, Anders.   For the round brackets, you need either to use a deferred block and then protect them from the regular expression handler's special meaning for them, or use double quotes and provide double protection.  Here's an example:

# Literally matching round brackets in regular expressions

# Use of {} as a deferred block
# ( and ) are taken literally
set value {Send me (his/her) data}

# If you use double quotes you must remember that a
# \ protects the character from the double quote operator
# so in the following case the protection doesn't get to
# the regular expression handler - thus acts as a set of
# capture parentheses just like the other brackets.

regexp "(.*)\((.*)\)(.*)" $value capall left mid right
puts "\nFailing Example"
puts "Left is $left"
puts "Right is $right"
puts "Mid is $mid"

# You can cure this by adding an extra \ for the regular
# expression handler, which itself needs to be protected
# from the double quote handler.

regexp "(.*)\\\((.*)\\\)(.*)" $value capall left mid right
puts "\nWorking Example - double protect in \"...\""
puts "Left is $left"
puts "Right is $right"
puts "Mid is $mid"

# Or you could use a deferred block (much easier unless you
# want to use variables in it too!) This way, you only need
# to protect your brackets from the regex handler

regexp {(.*)\((.*)\)(.*)} $value capall left mid right
puts "\nWorking Example - deferred block { .... }"
puts "Left is $left"
puts "Right is $right"
puts "Mid is $mid"



Here's the sample output. As Tcl's a pure interpretter, I
can add whatever I like, including sample output, onto the
end of my code!

Dorothy:may08 grahamellis$ tclsh brax

Failing Example
Left is Send me (his/her) data
Right is
Mid is

Working Example - double protect in "..."
Left is Send me
Right is  data
Mid is his/her

Working Example - deferred block { .... }
Left is Send me
Right is  data
Mid is his/her
Dorothy:may08 grahamellis$


Posted by eliasson (eliasson), 15 May 2008
Many thanx, I'll work with this :^).


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., 2021: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho