Reputation: 542
I have a TCL proc that I am trying to pass a variable through to a regular expression and it is not recognizing it.
proc _findit {RE} {
if [
regexp -all -nocase $RE $Line Match
] {
#DO STUFF
}
}
_findit \sword\s
Upvotes: 0
Views: 103
Reputation: 13252
The escapes are eliminated when the invocation of _findit
is evaluated, so the contents of the variable RE
is the string "swords"
. Try enclosing the pattern in braces, which will quote the backslashes and so preserve the escapes:
_findit {\sword\s}
Documentation for Tcl syntax, including rules for evaluating arguments is found here.
If you need to find out what your arguments look like inside the command you are invoking, add the following line inside the command:
puts [info level 0]
This prints the complete argument list, including the command name, after the invocation has been evaluated.
Edit to clarify the use of info level
:
I was a bit confused by the documentation for info level
at that time and what I wrote wasn't based on an entirely correct understanding (but luckily came out mostly right anyway). If called without an argument, info level
returns the stack level number of the current procedure call. If called with an argument (which must be a number) it returns the invocation (name and expanded arguments) for the stack level designated by the number. If the number is greater than 0, it is interpreted as an absolute stack level (1 being a procedure called from the top-level, 2 being a procedure called from stack level 1, etc). If it is less than one, it is interpreted as a relative stack level (0 being this procedure, -1 being the caller, -2 the caller's caller, etc). So, [info level [info level]]
and [info level 0]
give the same result (even when [info level]
isn't equal to 0), because they both indicate the current stack level in different ways.
This is similar to the numbering scheme used upvar
and uplevel
, except that "relative" stack levels are written 0, -1, -2, ... for info level
and 0, 1, 2, ... for up*
, while "absolute" stack levels are written 1, 2, 3, ... for info level
and #1, #2, #3, ... for up*
(making it possible to designate the top-level as #0).
Documentation: info
Upvotes: 4