Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How the heck to you match for [ ] { } characters?

163 views
Skip to first unread message

Christopher Freeman

unread,
May 8, 1996, 3:00:00 AM5/8/96
to

This may be a simple problem, but it's really causing me to pull my hair out.

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


John Haxby

unread,
May 8, 1996, 3:00:00 AM5/8/96
to

string match {*[][]*} $name

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.


Jan Wieck

unread,
May 8, 1996, 3:00:00 AM5/8/96
to

Christopher Freeman (clf...@nortel.ca) wrote:
: This may be a simple problem, but it's really causing me to pull my hair out.

: 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) #


Mike Hopkirk

unread,
May 8, 1996, 3:00:00 AM5/8/96
to

>This may be a simple problem, but it's really causing me to pull my hair out.
>
>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.
>
>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.
>


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


Jacques Marcoux

unread,
May 17, 1996, 3:00:00 AM5/17/96
to Christopher Freeman

>>>>> "Christopher" == Christopher Freeman <clf...@nortel.ca> writes:
In article <4morau$j...@nrtphc11.bnr.ca> Christopher Freeman <clf...@nortel.ca> writes:


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


0 new messages