I'm trying to make sure a string does NOT contain special characters like
braces, brackets, backslashes, etc. Basically, anything that would cause
Tcl to choke during simple string parsing is what I'm trying to weed out.
Here are some examples that do work:
% set name {This is a [String]}
This is a [String]
% string match {*\[*} $name
1
% string match {*\]*} $name
1
% string match {*[\[]*} $name
1
% string match {*[aeiou]*} $name
1
However, if I try to match both "[" and "]" at the same time:
% string match {*[\[\]]*} $name
0
I know I can always use a whole bunch of matching commands to match each
special character one at a time, but this is not a very elegant way to get the
job done.
Does anyone have a very quick way to match the special characters. I would
much appreciate the help.
Thanks.
Chris F
should work, though empirical evidence suggests it doesn't. On the other
hand
regexp {.*[][].*} $name
does work. The manual page explains why "[][]" matches any one square bracket.
--
John Haxby
These are my opinions, not my employer's.
: I'm trying to make sure a string does NOT contain special characters like
: braces, brackets, backslashes, etc. Basically, anything that would cause
: Tcl to choke during simple string parsing is what I'm trying to weed out.
: Here are some examples that do work:
: % set name {This is a [String]}
: This is a [String]
:
: % string match {*\[*} $name
: 1
: % string match {*\]*} $name
: 1
: % string match {*[\[]*} $name
: 1
: % string match {*[aeiou]*} $name
: 1
: However, if I try to match both "[" and "]" at the same time:
: % string match {*[\[\]]*} $name
: 0
Use regexp instead:
regexp {\[|\]|\{|\}|[\\()!"$%&/]} $name
matches [ ] { } \ ( ) ! " $ % & /
and returns 1 if any or these characters occurs in $name.
Until later, Jan
--
#define OPINIONS "they are all mine - not those of debis or daimler-benz"
#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me. #
#================================== wi...@sapserv.debis.de (Jan Wieck) #
Doesn't look like string match will do it - try regexp
regexp {.*[]\[].*} $name
1
Note the ordering of [] - the more obvious
regexp {.*[\[\]].*} $name
0
fails
--
- hops
Everything disclaimed (including disclaimer)
------< ho...@sco.com>--------------------------------------
Mike Hopkirk ( hops ) | Whenever possible steal code.
SCO Inc | Tom Duff. Bell Labs
Christopher> This may be a simple problem, but it's really causing me to pull my hair out.
[-------%<-------]
Christopher> Does anyone have a very quick way to match the special characters. I would
Christopher> much appreciate the help.
string match {*\[*\]*} $name
--
______ ______
___/ /_______________________________________/ /_
/_ __//_____//_Jacques_//_Marcoux_//_____/_ __/
/_/ /_/
jmar...@cmc.doe.ca 514.421.4794