Vishal Kirthic
Vishal Kirthic

Reputation: 21

Tcl regular expression to detect square brackets

I have a .csv files where many rows have one of the field values like this:

scl[0]
scl[1]
scl[2]
sda[1]
sda[2]
sda[3]

I am storing them in a variable while reading the csv files in line by line format,like:

 set string [$m get cell 0 1]

Now when I do regexp to check whether the cell has scl[0] I am unable to pass the square bracket to this regular expression: I gave this syntax:

if{[regexp "scl\[0\]" $string]} {
...
}

But the if condition doesn't get executed.

If in case of scl(0), i.e () instead of {} in csv file, I gave {[regexp "scl\[(\]0\[)\]" $string]} which worked. The same format I tried apply to square brackets still it doesn't get evaluated.

Am I missing something?

Please help.

Thanks

Upvotes: 2

Views: 3323

Answers (2)

Peter Lewerin
Peter Lewerin

Reputation: 13252

You could also use string equal: then you only need to worry about one level of quoting:

string equal {scl[0]} $string 

Documentation: string

Upvotes: 0

slebetman
slebetman

Reputation: 113906

Note that \ has special meaning inside double quotes. So just do:

regexp "scl\\[0\\]" $string

or:

regexp {scl\[0\]} $string

Upvotes: 3

Related Questions