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

Re: #% anchoring doesn't work with (S)

> On 02/02/2023 08:31 Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx> wrote:
> Could the bugbe fixed? It already makes #% pretty much unusable for a backward compatible software, yet in say 4 years this would be changed, if the bug would be fixed today
> On Mon, 30 Jan 2023 at 12:32, Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx> wrote:
> > INPUT=ABC; INPUT=${(S)INPUT//#%((#b)(*))/°match°}; print $match
> > #no output

It's a confusing combination of options but it looks like it's trying to do a shortest
match as if with a ${param#head} or ${param%tail} and so bailing out early when it's
found a substring.  This obviously doesn't work when we're anchoring at both the
start and the end, so tell it to do a longest match in that case.

We could probably do with some more tests with the #% combination, there aren't all
that many, so other oddities might be sneaking through.


diff --git a/Src/subst.c b/Src/subst.c
index 4ad9fee1a..3dd920e87 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -2926,6 +2926,9 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
 	    flags |= SUB_REST;
+	/* If matching at start and end, don't stop early */
+	if ((flags & (SUB_START|SUB_END)) == (SUB_START|SUB_END))
+	    flags |= SUB_LONG;
 	 * With ":" treat a value as unset if the variable is set but
diff --git a/Test/D04parameter.ztst b/Test/D04parameter.ztst
index a11652d1e..7990c2958 100644
--- a/Test/D04parameter.ztst
+++ b/Test/D04parameter.ztst
@@ -2307,6 +2307,13 @@ F:behavior, see http://austingroupbugs.net/view.php?id=888
+  a="string"
+  print ${(S)a//#%((#b)(*))/different}
+  print $match[1]
+0:Fully anchored string must be fully searched

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