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

weird "regsub" syntax

65 views
Skip to first unread message

Andreas Otto

unread,
Jun 4, 2018, 4:25:11 AM6/4/18
to
Hi,

all i want to do is…

# cut the prefix from a string
set prefix [regsub {^Mq[[:upper:]][[:lower:]]+} $name]

# on tcl I have to do
set prefix [regsub {^(Mq[[:upper:]][[:lower:]]+).*} $name {\1}]

#test…

proc funcDEF {name ret argv} {
...
set prefix [regsub {^(Mq[[:upper:]][[:lower:]]+).*} $name {\1}]
set prefix2 [regsub {Mq[[:upper:]][[:lower:]]+} $name {\0}]
set prefix3 [regsub {Mq[[:upper:]][[:lower:]]+} $name {\1}]
...
Print prefix prefix2 prefix3

#results

funcDEF -> prefix<MqBuffer>, prefix2<MqBufferAppendC>, prefix3<AppendC>,
funcDEF -> prefix<MqBuffer>, prefix2<MqBufferCastTo>, prefix3<CastTo>,
funcDEF -> prefix<MqBuffer>, prefix2<MqBufferCmp>, prefix3<Cmp>,
funcDEF -> prefix<MqBuffer>, prefix2<MqBufferCopy>, prefix3<Copy>,
funcDEF -> prefix<MqBuffer>, prefix2<MqBufferCreate>, prefix3<Create>,
funcDEF -> prefix<MqBuffer>, prefix2<MqBufferCreateB>, prefix3<CreateB>,
funcDEF -> prefix<MqBuffer>, prefix2<MqBufferCreateC>, prefix3<CreateC>,
funcDEF -> prefix<MqBuffer>, prefix2<MqBufferCreateD>, prefix3<CreateD>,
funcDEF -> prefix<MqBuffer>, prefix2<MqBufferCreateF>, prefix3<CreateF>,
funcDEF -> prefix<MqBuffer>, prefix2<MqBufferCreateI>, prefix3<CreateI>,
funcDEF -> prefix<MqBuffer>, prefix2<MqBufferCreateO>, prefix3<CreateO>,

→ only the "prefix"… the FIRST one give the right results…

#conclusion…

there is a requirement for a "regsub" switch… perhaps "-inline"

set prefix [regsub -inline {^Mq[[:upper:]][[:lower:]]+ $name]


mfg

AO

Arjen Markus

unread,
Jun 4, 2018, 6:45:23 AM6/4/18
to
On Monday, June 4, 2018 at 10:25:11 AM UTC+2, Andreas Otto wrote:
> Hi,
>
> all i want to do is…
>
> # cut the prefix from a string
> set prefix [regsub {^Mq[[:upper:]][[:lower:]]+} $name]

You are mistaken about the capturing of the substrings. \0 is the total matching substring, \1 is the substring captured by the pair of parentheses starting left-most, \2 the substring captured by the first pair parentheses starting after that etc.

What you are after is, I think:

set suffix [regsub {^Mq[[:upper:]][[:lower:]]+(.*)} $name {\1}]

that is, first determine what the prefix is and CAPTURE the rest.

Regards,

Arjen

heinrichmartin

unread,
Jun 4, 2018, 7:10:33 AM6/4/18
to
On Monday, June 4, 2018 at 12:45:23 PM UTC+2, Arjen Markus wrote:
> On Monday, June 4, 2018 at 10:25:11 AM UTC+2, Andreas Otto wrote:
> > # cut the prefix from a string
> > set prefix [regsub {^Mq[[:upper:]][[:lower:]]+} $name]
>
> set suffix [regsub {^Mq[[:upper:]][[:lower:]]+(.*)} $name {\1}]

> funcDEF -> prefix<MqBuffer>, prefix2<MqBufferAppendC>, prefix3<AppendC>,
> funcDEF -> prefix<MqBuffer>, prefix2<MqBufferCastTo>, prefix3<CastTo>,
> funcDEF -> prefix<MqBuffer>, prefix2<MqBufferCmp>, prefix3<Cmp>,
> funcDEF -> prefix<MqBuffer>, prefix2<MqBufferCopy>, prefix3<Copy>,
> funcDEF -> prefix<MqBuffer>, prefix2<MqBufferCreate>, prefix3<Create>,
> funcDEF -> prefix<MqBuffer>, prefix2<MqBufferCreateB>, prefix3<CreateB>,
> funcDEF -> prefix<MqBuffer>, prefix2<MqBufferCreateC>, prefix3<CreateC>,
> funcDEF -> prefix<MqBuffer>, prefix2<MqBufferCreateD>, prefix3<CreateD>,
> funcDEF -> prefix<MqBuffer>, prefix2<MqBufferCreateF>, prefix3<CreateF>,
> funcDEF -> prefix<MqBuffer>, prefix2<MqBufferCreateI>, prefix3<CreateI>,
> funcDEF -> prefix<MqBuffer>, prefix2<MqBufferCreateO>, prefix3<CreateO>,
>
> → only the "prefix"… the FIRST one give the right results…

I am not perfectly sure whether you want to fetch or cut the prefix ...

> set prefix [regsub {^Mq[[:upper:]][[:lower:]]+} $name]

You either copied the wrong thing here - or you received a "wrong # args" error...

set suffix [regsub {^Mq[[:upper:]][[:lower:]]+} $name ""] ;# replace the prefix with the empty string

set prefix [regexp -inline {^Mq[[:upper:]][[:lower:]]+} $name] ;# is empty if no match

if {[regexp {^Mq[[:upper:]][[:lower:]]+} $name prefix]} {
# work with prefix
}

Andreas Otto

unread,
Jun 4, 2018, 3:28:41 PM6/4/18
to
just to be clear I want to have the "prefix" in this example "MqBuffer" … my understanding is that the "reg-expr" (of regsub… or any other command)… fit the part of the string I WANT TO have… my perfect example would be…

set prefix [regsub {^Mq[[:upper:]][[:lower:]]+} $name]

with the "reg-expr" = {^Mq[[:upper:]][[:lower:]]+} is my WANT-TO-HAVE…

mfg.

Eric

unread,
Jun 4, 2018, 4:40:06 PM6/4/18
to
Well,

% set name "MqBuffer-27"
% regsub {^Mq[[:upper:]][[:lower:]]+} $name
wrong # args: should be "regsub ?-option ...? exp string subSpec ?varName?"

and

% regsub {^Mq[[:upper:]][[:lower:]]+} $name "XXXX"
XXXX-27

so you can't have what your perfect example (this has already been
said anyway)

% if [regexp {^Mq[[:upper:]][[:lower:]]+} $name prefix] {
puts $prefix
} else {
puts "no match"
}

is the neatest thing I can think of.

Eric
--
ms fnd in a lbry

heinrichmartin

unread,
Jun 4, 2018, 5:22:45 PM6/4/18
to
On Monday, June 4, 2018 at 9:28:41 PM UTC+2, Andreas Otto wrote:
> my understanding is that the "reg-expr" (of regsub… or any other command)

This is basically correct, but with regexp returning the match and regsub replacing it with sth else.

On Monday, June 4, 2018 at 10:40:06 PM UTC+2, Eric wrote:
> % if [regexp {^Mq[[:upper:]][[:lower:]]+} $name prefix] {
> puts $prefix
> } else {
> puts "no match"
> }

Stay used to expressions in braces (i.e. if {[regexp]}) for optimized byte code.

Donald Arseneau

unread,
Jun 10, 2018, 10:58:07 AM6/10/18
to
Andreas Otto <aotto1...@gmail.com> writes:

> all i want to do is…

Showing failing code is NOT telling what you WANT to do!
Nobody can really know what you intended from your code.

> # cut the prefix from a string
> set prefix [regsub {^Mq[[:upper:]][[:lower:]]+} $name]
>
> # on tcl I have to do
> set prefix [regsub {^(Mq[[:upper:]][[:lower:]]+).*} $name {\1}]

Assuming you want the prefix in the variable "prefix",
then my guess is you meant to use regexp, not regsub.
--
Donald Arseneau as...@triumf.ca
0 new messages