DurgaDatta
DurgaDatta

Reputation: 4170

Tcl: string matching the backslash

How can I match a backslash \ in Tcl? I did the follwoing:

% set a "\\"
\
% string length $a
1
% string match $a "\\"
0
% string match "\\" "\\"
0
% string match $a \\
0
% string match $a [set x "\\"]
0

But as seen above none of them help. I want to match $a.

Upvotes: 0

Views: 717

Answers (2)

Peter Lewerin
Peter Lewerin

Reputation: 13252

The correct way to match is

string match {[\]} \\
# => 1
string match {a[\]} a\\
# => 1

Documentation: string

(Note: the 'Hoodiecrow' mentioned in the comments is me, I used that nick earlier.)

Upvotes: 1

Steve Vinoski
Steve Vinoski

Reputation: 20004

If you look at the "Pattern Ending in Backslash" section in the string match documentation, it says:

A pattern ending in a backslash doesn't match a string ending in a backslash. Bug?
string match a\\ a\\
# -> 0

Using string equal works though:

% string equal $a "\\"
1

Upvotes: 1

Related Questions