
Matching strings that contain braces
Hi Martin,
Quote:
> Could somebody please explain why the following match command
> returns 0?
> tclsh
> % set b \\\{
> \{
> % string match $b $b
> 0
"string match $b $b" tries to match the _pattern_ $b (first arg) with
the _string $b (second arg). If you want to compare string, use "string
compare". But I guess this is not your goal here.
Quote:
> The regexp command exhibits equally strange behavior. I have yet to
> find any pattern that will match a string that contains an escaped
> brace. We are using TCL version 7.5b3.
The problem is not with the escaped brace but with the backslash.
"string match" does a special interpretation of the characters *?[]\ in
the pattern, so you have to escape them:
% set b \\{
\{
% # note that the 3rd '\' is useless in your code: since the brace is
not the 1st character of the word, you don't have to escape it. See the
Tcl evaluation rules for more details.
% set pat \\\\{
\\{
% string match $pat $b
1
The problem is the same for regexp:
% regexp $b $b v
1
% set v
{
% # Arg! Where is the '\'?
% regexp $pat $b v
1
% set v
\{
% # It works!
See you, Fred
--
Ingenieur Ecole des Mines de Nantes/Ecole des Mines de Nantes Engineer
IRISA Rennes, France - Projet Solidor/Solidor Project
------------------------------------------------------------------------
Tcl: can't leave | "Il ne faut jamais remettre au lendemain ce
$env(HOME) without it! | qu'on peut faire le surlendemain" (Oscar WILDE)