Gordon
Gordon

Reputation: 1651

regex with tcl and $ in variable value

I need help with this regex for tcl. I want to detect the $ character but it isn't flagging. Any ideas?

set cell {ABC_ONE_123_$12345$wc_PIE_IN_SKY}
string match $ $cell

Upvotes: 1

Views: 822

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385900

string match and glob patterns

string match does a match against a glob pattern, not a regular expression. Plus, it will try to match the whole string. The glob pattern $ doesn't match since the string has much more than just a dollar sign. However *$* does, since it says "zero or more characters, a dollar sign, and zero or more characters". Because $ is treated specially by the tcl shell, you must quote it properly.

For example:

% string match {*$*} $cell
1
% string match *\$* $cell
1

regular expressions

If you really want to do a regular expression search rather than a glob pattern match, use the regexp command. In this case, you must a) protect the $ from normal tcl interpretation just like with string match, and b) because it is special to regular expressions, you must protect the dollar sign from regex interpretation.

Here's an example:

% regexp {\$} $cell
1
% regexp \\\$ $cell
1

Upvotes: 4

Related Questions