Why does the following not match the :
expect {
timeout {puts timedout\n}
\[1\]: {puts matched\n}
}
> expect test.tcl
[1]:
timedout
If I change it and remove the colon the match works:
expect {
timeout {puts timedout\n}
\[1\] {puts matched\n}
}
$ expect test.tcl
[1]
matched
Or if I get rid of the 1st bracket
expect {
timeout {puts timedout\n}
1\]: {puts matched\n}
}
then it matches:
$ expect test.tcl
1]:
matched
It is not the problem with
:, but with[.The
[is special to bothTcland theExpectpattern matcher so it is particularly messy. To match a literal[, you have to backslash once fromTcland then again so that it is not treated as a range during pattern matching. The first backslash, of course, has to be backslashed to prevent it from turning the next backslash into a literal backslash!So, your code should be,
You can prefix the
]with a backslash if it makes you feel good, but it is not necessary. Since there is no matching left-hand bracket to be matched within the double-quoted string, nothing special happens with the right-hand bracket. It stands for itself and is passed on to theExpectcommand, where it is then interpreted as the end of the range.The next set of examples shows the behavior of
[as a pattern preceded by differing numbers of backslashes. If the[is not prefixed by a backslash,Tclinterprets whatever follows as a command. For these examples, imagine that there is a procedure namedXYthat returns the stringn*w.The
\\[XY]case deserves close scrutiny.Tclinterprets the first backslash to mean that the second is a literal character.Tclthen producesn*was the result of the XY command. The pattern matcher ultimately sees the four character stringn*w. The pattern matcher interprets this in the usual way. The backslash indicates that thenis to be matched literally (which it would even without the backslash since the n is not special to the pattern matcher).Source : Exploring Expect