Article View: comp.lang.tcl
Article #48Re: A behaviour of regexp I cannot understand
From: Rich
Date: Wed, 14 Oct 2026 19:21
Date: Wed, 14 Oct 2026 19:21
56 lines
3370 bytes
3370 bytes
Olivier <user1108@newsgrouper.org.invalid> wrote:
>
> Hello,
>
> Simply :
>
> set _res [regexp -inline -all {\S+} "dum dummy a b c d c4"]
\S is a special symbol, it is documented in the re_syntax man page
under the "CLASS-SHORTHAND ESCAPES" header:
\S [^[:space:]]
So using \S is the same as having entered [^[:space:]] into the regex.
And that [^[:space:]] means "any character that is not in the set
":space:" (whitespace). Or shorter: "not whitespace characters".
"+" in a regex means one or more matches of the prior atom (in this
instance the shorthand for "[^[:space:]]".
So \S+ means one or more characters that are not whitespace characters.
Which is why you get this result:
> --> gives a list of 7 items : dum dummy a b c d c4
>
> set _res [regexp -inline -all {c+} "dum dummy a b c d c4"]
"c" is just a regular character. So it means "c". + means one or more
of the prior atom (the "c") so it means a run of one or more c
characters. You have two runs of "c" characters, both one character
long. The lone "c" and the "c4". So this result also makes perfect
sense:
> --> gives a list of " items : c c
>
> I understand that {c+} acts as a specification, but I cannot understand why {\S+}
> acts as a counter-specification, it should return only spaces and/or tabs ?
/S+ (the capitolized variety) is already a "not" specifier by
definition. So it is "not whitespace" which is why you get everything
but the white space. You may be thinking of \s which is shorthand for [[:space:]]
which is "all white space characters".
> I found this "trick" on
> https://wiki.tcl-lang.org/page/Get+a+Certain+Line+From+a+File I often
> deal with simulation data where items of line are homogeneously
> separated either by space or by tab. So the line can be considered
> as a list. But in rare case you stumble on hybrid separators : line
> defining units or names for instance, that is how I came to this
> question. And this "trick" works as a charm !
Yes, it's a useful "trick" to normalize a set of "white space
separatated items" into a list, when you don't care how many white
space separaters there are, or which exact white space characters are
present.
Technical Details
Message-ID:
<11aokom$1pc17$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<1791990352-1108@newsgrouper.org>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: Rich <rich@example.invalid>
Newsgroups: comp.lang.tcl
Subject: Re: A behaviour of regexp I cannot understand
Date: Wed, 14 Oct 2026 19:21:58 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <11aokom$1pc17$1@dont-email.me>
References: <1791990352-1108@newsgrouper.org>
Injection-Date: Wed, 14 Oct 2026 19:22:01 +0000 (UTC)
Injection-Info: dont-email.me;
logging-data="1880103"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18yOhAQwxHehqAy4S38syK5"; posting-host="aa35b32e43f7ff453a6f4eed126df023"
User-Agent: tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.193 (x86_64))
Cancel-Lock: sha1:DoPGGEbPnuVCGVLXhkGV2dCoyIM= sha256:v/zJ+dz9clJYAhcHGSS+7jcpzD2Q8De9O7hpRMCjaFM=
sha1:vVz5veRoNXDxBfDTEEY/RpyBYfA= sha256:hXLWmZeBAxrvhFVyhhsQBRBSJmn0c68OLnV5ZXWgFgQ=