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

Matching ']' in glob patterns

81 views
Skip to first unread message

bmbur...@gmail.com

unread,
Jun 3, 2012, 12:18:36 PM6/3/12
to

George Petasis

unread,
Jun 3, 2012, 1:01:36 PM6/3/12
to bmbur...@gmail.com
Στις 3/6/2012 19:18, ο/η bmbur...@gmail.com έγραψε:
> Can anyone answer my question that I posted here: http://stackoverflow.com/questions/10871855/tcl-match-closing-bracket-in-glob-pattern

You can use braces:

set x {]}

switch -glob $x {
{]} {puts "MATCH \]"}
}

George

Eugene

unread,
Jun 3, 2012, 1:14:48 PM6/3/12
to
or just shield a bracket with a backslash: set x \]

--
Best regards, Eugene.

Uwe Klein

unread,
Jun 3, 2012, 1:04:01 PM6/3/12
to
set pattern abc\]def
set glob *\\\]*

puts stderr glob:$glob\ pattern:$pattern

switch -glob $pattern \
$glob {
puts stderr glob:$pattern
} default {
puts stderr nomatch
}


uwe

bmbur...@gmail.com

unread,
Jun 3, 2012, 2:23:08 PM6/3/12
to
All these options are not the same as the question. I need to match the right bracket as part of a character set. I need to match all types of brackets, and so am using the glob "[()[]{}]", which of course doesn't work.

Does Tcl have fall-through handling of cases? As a workaround I could match the string with the character class containing all the other types, and then match this one specific case separately and fall through.

George Petasis

unread,
Jun 3, 2012, 3:01:42 PM6/3/12
to bmbur...@gmail.com
Your question was not clear. Have you looked at the manual of switch?

switch -regexp $x {
{[\(\)\[\]\{\}]} {puts match}
}

George

bmbur...@gmail.com

unread,
Jun 3, 2012, 3:11:13 PM6/3/12
to bmbur...@gmail.com
Thank you. I don't know why I didn't think of using regex instead of glob. But this seems to be a bug in Tcl that the ']' can't be escaped in a glob pattern character set.

Alexandre Ferrieux

unread,
Jun 3, 2012, 3:12:51 PM6/3/12
to
On Jun 3, 8:23 pm, bmburst...@gmail.com wrote:
> I need to match the right bracket as part of a character set. I need to match all types of brackets, and so am using the glob "[()[]{}]", which of course doesn't work.

To my knowledge, this is not possible directly with glob matching.
However, it is easy with regexp matching: just put the "]" as first in
range:

regexp {[][(){}]} $x

See the "BRACKET EXPRESSIONS" section in the re_syntax manpage.

-Alex

George Petasis

unread,
Jun 3, 2012, 4:10:40 PM6/3/12
to
Στις 3/6/2012 22:11, ο/η bmbur...@gmail.com έγραψε:
>
> Thank you. I don't know why I didn't think of using regex instead of glob. But this seems to be a bug in Tcl that the ']' can't be escaped in a glob pattern character set.

I am not sure I understand. Why can't ] be escaped?
\] is a normal character, and can be used in any glob pattern.

George

Alexandre Ferrieux

unread,
Jun 3, 2012, 5:09:26 PM6/3/12
to
On Jun 3, 10:10 pm, George Petasis <petas...@yahoo.gr> wrote:
> Στις 3/6/2012 22:11, ο/η bmburst...@gmail.com έγραψε:
>
>
>
> > Thank you. I don't know why I didn't think of using regex instead of glob. But this seems to be a bug in Tcl that the ']' can't be escaped in a glob pattern character set.
>
> I am not sure I understand. Why can't ] be escaped?
> \] is a normal character, and can be used in any glob pattern.

Apparently not:

% string match {[\]a]} a
0

-Alex

Andreas Leitgeb

unread,
Jun 4, 2012, 6:37:18 AM6/4/12
to
Alexandre Ferrieux <alexandre...@gmail.com> wrote:
> On Jun 3, 8:23 pm, bmburst...@gmail.com wrote:
>> I need to match the right bracket as part of a character set.
>> I need to match all types of brackets, and so am using the
>> glob "[()[]{}]", which of course doesn't work.
> To my knowledge, this is not possible directly with glob matching.

FWIW, in unix-shell (bash - probably others, too):
$ touch 'abc[def]ghi'
$ echo *[]]*
abc[def]ghi

Was it an oversight in the specification of Tcl's glob, or was it
deliberately left out?

Alexandre Ferrieux

unread,
Jun 4, 2012, 8:13:07 AM6/4/12
to
On Jun 4, 12:37 pm, Andreas Leitgeb <a...@gamma.logic.tuwien.ac.at>
wrote:
From a 'fossil annotate' on tclUtil.c, function Tcl_StringCaseMatch:

16f0a0d493 1999-12-04 hobbs: }
16f0a0d493 1999-12-04 hobbs: while (1) {
16f0a0d493 1999-12-04 hobbs: if ((*pattern == ']') || (*pattern
== '\0')) {
16f0a0d493 1999-12-04 hobbs: return 0;
16f0a0d493 1999-12-04 hobbs: }

(and the lack of special casing nor comments to that regard)
... it seems that this Day One limitation comes from the fact that
nobody cared (nor cares, to be frank) about strict compatibility with
shell globbing. In hindsight, 99% compat is okay if the 1% has a
noticeable complexity cost.

-Alex

Andreas Leitgeb

unread,
Jun 4, 2012, 3:34:29 PM6/4/12
to
Alexandre Ferrieux <alexandre...@gmail.com> wrote:
>> FWIW, in unix-shell (bash - probably others, too):
>> $ touch 'abc[def]ghi'
>> $ echo *[]]*
>> abc[def]ghi
>> Was it an oversight in the specification of Tcl's glob, or was it
>> deliberately left out?
> From a 'fossil annotate' on tclUtil.c, function Tcl_StringCaseMatch:
> 16f0a0d493 1999-12-04 hobbs: while (1) {
> 16f0a0d493 1999-12-04 hobbs: if ((*pattern == ']') || (*pattern
>== '\0')) {
> 16f0a0d493 1999-12-04 hobbs: return 0;
> 16f0a0d493 1999-12-04 hobbs: }
> (and the lack of special casing nor comments to that regard)
> ... it seems that this Day One limitation comes from the fact that
> nobody cared (nor cares, to be frank) about strict compatibility with
> shell globbing.

I wouldn't care about []], if at least there was [\]] in glob patterns.

"Strict compatibility with shell-globbing" surely isn't all that important,
but being able to include close-brackets in character sets would be really
neat, and the implementation would be quite trivial:

change (in the quoted fossil snippet)
if ((*pattern == ']') || (*pattern == '\0')) {
to
if ( (*pattern == '\0') ||
( (*pattern == ']') && pattern > firstsetcharpos ) ) {
and set firstsetcharpos to pattern+1 where the open '[' is hit.

Actually, there is another dirty hack for the OP:
glob {*{[[{}()],]}*}
but that finds a file with both [ and ] in its name twice. ;-)

> In hindsight, 99% compat is okay if the 1% has a
> noticeable complexity cost.

Agree, but does it?

I figure you'll point out documentation now:
"A ] may be matched by including it as the first character in the set."
That's how bash describes it.

0 new messages