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

possible "regsub/regexp" bug

42 views
Skip to first unread message

aotto1968

unread,
Jun 30, 2022, 4:54:04 AM6/30/22
to
Hi,

I have the following regexp:

> "(?!\()([A-Z][a-z]+)(?!\))"

and I want to match

> "MkObjectLogC"

into:

> "(?:Mk)?(?:Object)?(?:Log)?C"

using the code

> set match "MkObjectLogC"
> while {[regsub {(?!\()([A-Z][a-z]+)(?!\))} $match {(?:\1)?} match]} {}

but the solution is:

> "(?:Mk)?(?:(?:(?:(?:(?:Ob)?j)?e)?c)?t)?(?:(?:Lo)?g)?C"

the online regexp is ok:

> https://regex101.com/r/cWJrDK/1

mfg

Schelte

unread,
Jun 30, 2022, 6:40:46 AM6/30/22
to
On 30/06/2022 10:53, aotto1968 wrote:
> using the code
>
> > set match "MkObjectLogC"
> > while {[regsub {(?!\()([A-Z][a-z]+)(?!\))} $match {(?:\1)?} match]} {}
>
> but the solution is:
>
> > "(?:Mk)?(?:(?:(?:(?:(?:Ob)?j)?e)?c)?t)?(?:(?:Lo)?g)?C"
>
> the online regexp is ok:
>
That's because you repeatedly reprocess the result of the previous
substitution. The online regexp does the processing in one go. If you do
the same with regsub, you get the requested result:

regsub -all {(?!\()([A-Z][a-z]+)(?!\))} $match {(?:\1)?} match


Schelte.

clt.to...@dfgh.net

unread,
Jun 30, 2022, 12:28:40 PM6/30/22
to

>That's because you repeatedly reprocess the result of the previous
>substitution. The online regexp does the processing in one go. If you do
>the same with regsub, you get the requested result:
>
> regsub -all {(?!\()([A-Z][a-z]+)(?!\))} $match {(?:\1)?} match
>

Why are the (?!\() bits necessary?

regsub -all {([A-Z][a-z]+) $match {(?:\1)?}

seems to work

Dave B







Schelte

unread,
Jun 30, 2022, 3:01:36 PM6/30/22
to
On 30/06/2022 18:28, clt.to...@dfgh.net wrote:
>> That's because you repeatedly reprocess the result of the previous
>> substitution. The online regexp does the processing in one go. If you do
>> the same with regsub, you get the requested result:
>>
>> regsub -all {(?!\()([A-Z][a-z]+)(?!\))} $match {(?:\1)?} match
>>
>
> Why are the (?!\() bits necessary?
>
They're not. But I tend to change as little as possible from the
original code to focus on the problem at hand.

Why do you start a new thread, instead of following up in the existing one?


Schelte.
0 new messages