Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[BUG] `$match` is haunting my regex’s trailing, optional, capture



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






Messages sorted by: Reverse Date, Date, Thread, Author