Matching strings that contain braces 
Author Message
 Matching strings that contain braces

Could somebody please explain why the following match command
returns 0?

  tclsh
  % set b \\\{
  \{
  % string match $b $b
  0

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.




Sun, 25 Jul 1999 03:00:00 GMT  
 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)



Mon, 26 Jul 1999 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. string match fails for strings containing "["

2. Parsing a CSV file containing quoted strings (themselves containing commas)

3. possible bugs with comments containing braces

4. String Match - not matching

5. regular expression for nested matching braces

6. Problem with string module and strings containing \0

7. Matching and comparison of fields containing numbers

8. Innermost containing tag: match/replace it

9. Brackets in brackets, braces in braces

10. Brackets in brackets, braces in braces

11. Braces within a string

12. passing a prolog string to tcl and unbalanced braces

 

 
Powered by phpBB® Forum Software