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

Posted by eliasson (eliasson), 29 April 2008
Hello,

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?

TIA,


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

Thx,
Anders

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:

Code:
# 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"

exit

---------------------------

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$


See http://www.wellho.net/resources/ex.php4?item=t205/brax.

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

Anders



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.

You can Add a comment or ranking to this page

© 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