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
Python, Lua, Tcl, C and C++ training - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, Ruby, MySQL and Linux / Tomcat systems
regexp for multi line matching

Posted by ahmedrubbani (ahmedrubbani), 24 November 2006
Helllo Everybody,
                         I am a new memeber of this forum and a very new to TCL programming. I need immediate help from you guys. I am sure somebody will help me out. My question is fairly basic. I am trying to write regexp that will only match the following string (When
it is on a line entirely by itself.):

Please let me know how can I modify my program for multiple line string e.g "ads\nABC\nere". My code just to match ABC in single line is given below. PLEASE HELP ME OUT.

set match 0;

#define a string
set something "ABC";
puts "STRING ENTERED: $something";

#Determine length of string (something) and store it in a
set a [string length $something];

#matches the string with ABC
regexp ABC $something match;

#conditional check if string is ABC AND its length is 3
if {$match eq "ABC" && $a eq 3} {
puts "Matched: $match"} else {puts "Not Matched";}

Posted by admin (Graham Ellis), 25 November 2006
In regular expressions, ^ and $ usually match at the very start or at the vey end of an expression. However, if you use what is know as "multiline" mode, you can modify their behavious so that they match at embedded new lines.   In the case of Tcl, this is done with the -lineanchor option.  Thus:

set samples [list "Hello world\nABC\nThis matches" \
       "Another test\nABCD\nNo match" ]

foreach sample $samples {
       puts [regexp -lineanchor {^ABC$} $sample]

Posted by ahmedrubbani (ahmedrubbani), 26 November 2006

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