Tcl’s regexp Command
Tcl’s regexp command matches strings using regular expressions. The regexp command has the same internal pattern matcher that expect uses but the interface is different. The expect command provides the string implicitly while regexp requires that the string be an explicit argument.
The calling syntax is as follows:
regexp pattern string var0 var1 var2 var3. . .
The first argument is a pattern. The second argument is the string from which to match. The remaining arguments are variables, set to pieces of the string that match the pattern. The variable var0 is set to the substring that was matched by the whole pattern (analogous to expect_out(0,string)). The remaining variables are set to the substrings that matched the parenthesized parts of the pattern (analogous to expect_out(1,string) through expect_out(9,string)).
expect1.1> set addr "usenet@uunet.uu.net"For example, the following command separates the Internet email address (above) into a user and host:
expect1.2>regexp (.*)@(.*) $addr ignore user host1 expect1.3>set userusenet expect1.4>set hostuunet.uu.net
The first parenthesized pattern matches the user and is stored in the variable user. The @ matches the literal @ in the address, and the remaining parenthesized pattern matches the host. Whatever is matched by the entire pattern is stored in ignore, called this because it is not of interest here. This is analogous to the expect command where expect_out(0,string) is often ignored. The command returns ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access