Azad Mamedov
Azad Mamedov

Reputation: 57

two asterisk sttring match in TCL

Help me to decide one problem in TCL. By using my macros I want find string, which contains two asterisk (**). I tried to used following commands:

string match \*\* string_name

But it doesn't work. Can you explain me where I made a mistake and how to do it correctly?

Thanks in advance!

Upvotes: 2

Views: 961

Answers (1)

Jerry
Jerry

Reputation: 71538

What you are actually passing to the interpreter is string match ** string_name. You need to pass the actual backslashes to the interpreter so that it then will understand two escaped asterisks, and to do that you need to add a couple more backslashes:

string match \\*\\* $s

Or use braces:

string match {\*\*} $s

Note that the above will match only if $s contains 2 asterisks, and nothing else. To allow for anything before and after the asterisks, you can use more asterisks...

string match {*\*\**} $s

There are a few other ways to check if a string has double asterisks, you can for instance use string first (and since this one does not support expressions, you can actually get away without having to escape anything):

string first ** $s

If you get something greater than -1, then ** is present in $s.

Or if you happen to know some regular expressions:

regexp -- {\*\*} $s

Those are the most common I think.

Upvotes: 3

Related Questions