I'm not sure why you need to use regexp
to do that. You could use string methods, such as string match
which uses glob matching:
string match {*[0-9]*} $a
*
will match anything, it's a wildcard in glob matching.
Or you could use string first
with expr
, which basically returns 1 if the returned index is above -1 (i.e. the [0-9]
is in the string $a
) or 0 otherwise (i.e. there is no [0-9]
in the string $a
):
expr {[string index {[0-9]} $a] > -1}
Anyway, if you really want a regex, you'll have some escaping to do:
regexp {\[0-9\]} $a out
Note that you won't need the $out
since you are looking for an exact match of something that cannot change, so you might as well use regexp {\[0-9\]} $a
Or use a syntax which tells regexp
to treat the string literally:
regexp {***=[0-9]} $a
or
regexp {(?q)[0-9]} $a
Where ***=
and (?q)
tells regexp
to treat the following characters as literal ones.
You can learn more about the re syntax on this man page.
But strangely when I use "" I can only match only 0
That's because what you are passing to the regexp
engine is [0-9]
. This is because the \[
and \]
are being substituted before passing the expression to regexp
. So you are actually having the expression [0-9]
being passed, which will match any single digit.