You're very close:
set stringToTest "AXIOMB"
set match [regexp {^A([C-Z]*)B$} $stringToTest -> substring]
if {$match} {
puts "The middle was $substring"
}
(The ->
is actually an unusual variable name. But here I'm using that symbol because it looks better than using the otherwise-equivalent someRandomDummyVariable
. :-))
If you're seeking to get the string to test from the command line or the console, here's how:
Command line arguments (without the name of the Tcl interpreter or the script) are presented as a list in the global argv
list variable. The first one is thus [lindex $::argv 0]
.
A line can be read from the console via the gets
command.
set line [gets stdin]; # you can use other channel names too, of course
Note that, unlike in C, gets
in Tcl is strongly defended against buffer overflows and the (almost) full power of scanf()
is about equivalent to scan [gets stdin] ...
(except for some formats excluded for security reasons).