On 05/02/2018 02:23 PM, asilver wrote:
> I've used lsearch quite a bit but can't understand why this is not working as expected.
You're suffering from double substitution, but not realizing it.
> Here is a simple list:
> % set cmd_list {"show interface" " BREAK "}
> "show interface" " BREAK"
>
> Directly below, this works as expected:
> % lsearch -regexp $cmd_list "\s*BREAK\s*"
> 1
The pattern you are matching is {s*BREAKs*}. Tcl does backslash
substitution in quoted words before [lsearch] ever sees the argument.
That matches because an empty string matches (s*) on either end and the
literal BREAK matches in the middle.
What you meant to code was:
% lsearch -regexp $cmd_list {\s*BREAK\s*}
1
When you make the same correction to your other examples:
% lsearch -regexp $cmd_list {^\s*BREAK\s*}
1
% lsearch -regexp $cmd_list {\s*BREAK\s*$}
1
Without the correction, they fail because anchoring to the string
beginning or end means the space sequences cannot be ignored and
the pattern (s*) does not match sequences of spaces.
--
| Don Porter Applied and Computational Mathematics Division |
|
donald...@nist.gov Information Technology Laboratory |
|
http://math.nist.gov/~DPorter/ NIST |
|______________________________________________________________________|