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

missing feature for "regsub"

53 views
Skip to first unread message

aotto1968

unread,
Jun 24, 2022, 4:02:13 AM6/24/22
to
Hi,

*regsub* and *regexp* are used to apply a *regexpression* on a string.
In difference to *regexp* the *regsub* is used to modify the string.

I have the following problem:

→ I want to modify the string *config_error_text* to *configErrorText*.

this looks like a simple *regsub* job like:

> regsub -all {_(\w)} "config_error_text" {\1??}

but as you see I can not uppercase the '\1' to just do the job I want.
I cant even call a *proc* in the *{\1??}* to do an arbitrary
post-processing.


mfg

Arjen Markus

unread,
Jun 24, 2022, 8:46:13 AM6/24/22
to
If I remember correctly, Tcl 8.7 does allow you to specify a procedure like that.

Regards,

Arjen

Gerald Lester

unread,
Jun 24, 2022, 11:04:22 AM6/24/22
to
You may want to consider using the *string map* command instead.

--
+----------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+----------------------------------------------------------------------+

clt.to...@dfgh.net

unread,
Jun 24, 2022, 11:10:01 AM6/24/22
to

The Tcl string functions are often faster than regexp (and regsub) for fairly complex string manipulations.

For instance to convert snake to camel case:

join [lmap x [split $str _] {string toupper $x 0 0}] ""

is faster than:

regsub -all {_(\w)} $str {\1}

And the regsub only removes the "_", it does not capitalize the following character.

There are some differences in the results for "odd" cases. The first one will eat multiple or trailing "_", and the regexp will leave some of them.

Dave B

Siri Cruise

unread,
Jun 24, 2022, 12:35:59 PM6/24/22
to
In article
<a81653bf-f4f0-4532...@googlegroups.com>,
Arjen Markus <arjen.m...@gmail.com> wrote:

> > this looks like a simple *regsub* job like:
> >
> > > regsub -all {_(\w)} "config_error_text" {\1??}
> >
> > but as you see I can not uppercase the '\1' to just do the job I want.
> > I cant even call a *proc* in the *{\1??}* to do an arbitrary
> > post-processing.
> >
> >
> > mfg
> If I remember correctly, Tcl 8.7 does allow you to specify a procedure like
> that.

Making every conceivable and inconceivable (and I do know its
meaning) edit possible results in a bloat of never used options.

while {[regexp {^(.*)_(¥w)(.*)$} $string - pre letter post]} {
set string $pre[string toupper $letter]$post
}

And if I do uae this pattern a lot:
proc regall {string re args} {
set script [lindex $args end]
set vars [lrange $args 0 end-1]
while {[llength [set vals [regexp -inline $string $re]]]}
{
uplevel 1 [list lassign [lrange $vals 1 end] $vars]
set string [uplevel 1 $script]
}
return $string
}

regall $string {^(.*)_(¥w)(.*)$} pre letter post {concat
$pre[string toupper $letter]$post]}

--
:-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted. @
'I desire mercy, not sacrifice.' /|¥
Discordia: not just a religion but also a parody. This post / ¥
I am an Andrea Chen sockpuppet. insults Islam. Mohammed
0 new messages