zsh-workers
 help / color / mirror / code / Atom feed
From: chris0e3@gmail.com
To: zsh-workers@zsh.org
Subject: [BUG] `$match` is haunting my regex’s trailing, optional, capture
Date: Sat, 9 Dec 2023 05:14:18 +0000	[thread overview]
Message-ID: <A231AE39-13BE-487E-AE31-AF35F2891A8C@gmail.com> (raw)

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




             reply	other threads:[~2023-12-09  5:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-09  5:14 chris0e3 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=A231AE39-13BE-487E-AE31-AF35F2891A8C@gmail.com \
    --to=chris0e3@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).