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
 
This week, we're updating our course layouts and descriptions. Presentation and materials always gently change over time, but just occasionally there's a need to make a step change to clear out some of the old and roll in the new. That's now happening - but over a long and complex site it's not instant and you'll see sections of the site changing up to and including 19th September.

See also [here] for status update
 
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.):

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

#Initialisation
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:

Code:
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
Thanks



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., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho