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

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



On Fri, Dec 8, 2023 at 10:23 PM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
On Fri, Dec 8, 2023 at 9:14 PM <chris0e3@xxxxxxxxx> wrote:
>
>   setopt rematch_pcre
>   [[ 'REQUIRE. OPT' =~ 'REQUIRE.(\s*OPT)?' ]] && printf '\tA. ‹%s›\n' $match
>   [[ 'REQUIRE.'     =~ 'REQUIRE.(\s*OPT)?' ]] && printf '\tB. ‹%s›\n' $match

[...] I can reproduce your example with the most recent git checkout, as
well.  Oliver recently updated to pcre2, so if a patch appears, you
may be on your own to backport it.

This was easier to track down than I feared, but possibly difficult to handle correctly.
 
Is "unset match" OK here?  There doesn't seem to be an obvious way to distinguish "there are capture expressions, but none matched anything" from "there were no capture expressions".  Maybe Oliver has a better clue.

diff --git a/Src/Modules/pcre.c b/Src/Modules/pcre.c
index e48ae3ae5..e4cdd8dbd 100644
--- a/Src/Modules/pcre.c
+++ b/Src/Modules/pcre.c
@@ -210,7 +210,8 @@ zpcre_get_substrings(pcre2_code *pat, char *arg, pcre2_match_data *mdata,
 	    }
 	    *x = NULL;
 	    setaparam(substravar, matches);
-	}
+	} else if (substravar)
+	    unsetparam(substravar);
 
 	if (namedassoc
 		&& !pcre2_pattern_info(pat, PCRE2_INFO_NAMECOUNT, &ncount) && ncount


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