zsh-workers
 help / color / mirror / code / Atom feed
* [BUG] `$match` is haunting my regex’s trailing, optional, capture
@ 2023-12-09  5:14 chris0e3
  2023-12-09  6:23 ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: chris0e3 @ 2023-12-09  5:14 UTC (permalink / raw)
  To: zsh-workers

Hello,

I’m using a custom built zsh 5.9 & PCRE 8.45 on macOS.
I’m seeing unexpected values in `$match` after a successful match.

What is the expected output of:
```
  setopt rematch_pcre
  [[ 'REQUIRE. OPT' =~ 'REQUIRE.(\s*OPT)?' ]] && printf '\tA. ‹%s›\n' $match
  [[ 'REQUIRE.'     =~ 'REQUIRE.(\s*OPT)?' ]] && printf '\tB. ‹%s›\n' $match
```

I had expected:
```
	A. ‹ OPT›
	B. ‹›
```

But I get:
```
	A. ‹ OPT›
	B. ‹ OPT›
```

Reversing the order of the tests (& executing them in a new Terminal window) produces expected/different results. [Though executing in a sub-shell appears to inherit the previous value of `$match`.  Is that expected?]  So this is probably just due to `$match` initially being empty.
However, changing the regex to 'REQUIRE.(\s*OPT)?(.*)' or '(REQUIRE).(\s*OPT)?' produces expected results.

It looks like: if there is a match, but no captures are matched then `$match` is not cleared.  However, I think it should be cleared.  The zsh manual §22.23 appears to imply what I contend.  [If I read it correctly.]

Based on my hypothesis I wrote this (simplification):
```
  setopt rematch_pcre; match=RUBBISH
  [[ A =~ 'A|(B)' ]] && printf '\ta. ‹%s›\n' $match
  [[ B =~ '(A)|B' ]] && printf '\tb. ‹%s›\n' $match
```

I would expect:
```
	a. ‹›
	b. ‹›
```

But I get:
```
	a. ‹RUBBISH›
	b. ‹RUBBISH›
```
[But changing the regexes to 'A()|(B)' & '(A)|B()' produces the expected results.]


So.  Am I right?  And is it possible to fix zsh?  Or am I wrong?

Thanks,

CHRIS




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-01-25 22:14 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-09  5:14 [BUG] `$match` is haunting my regex’s trailing, optional, capture chris0e3
2023-12-09  6:23 ` Bart Schaefer
2023-12-09 20:54   ` [PATCH?] " Bart Schaefer
2023-12-11 23:49     ` Oliver Kiddle
2023-12-12  1:38       ` Bart Schaefer
2024-01-25 22:14         ` Oliver Kiddle

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).