Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: [PATCH v6] regexp-replace and ^, word boundary or look-behind operators (and more).
- X-seq: zsh-workers 54698
- From: Mikael Magnusson <mikachu@xxxxxxxxx>
- To: Zsh hackers list <zsh-workers@xxxxxxx>
- Subject: Re: [PATCH v6] regexp-replace and ^, word boundary or look-behind operators (and more).
- Date: Sun, 7 Jun 2026 12:36:53 +0200
- Arc-authentication-results: i=1; mx.google.com; arc=none
- Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=90T9YqWQnfWUJN5t987vuXt5nET1QSFc612adq19W60=; fh=BgAYDYpL6Ne/A5nWEMVJiHiBtrz8Imz3uf26RDwgQX4=; b=SPvdwki9A6jEtwdZFGypJ0AWdoEu84kS+4MRat90sRe91GA3EYG4WWJAPCZygpl41v Nf8wf9y+lJChO9rayKpC1Keg6hm/0cXLTrXEGCto6XDp2uIPdta2lQYU/UHBuMLZeOGp EZpCe62zgViphins2RMiAziTB1Ks43OC56am0SlQTjlMZMMS/vXUEtvsL2wNlmBxidgN pZ/kFfmspZELoi7P0oVeBd5xxdYSm8GaCHWCK2GKU79XWQsofKTB2i5OTelKZ1do8hKS hyI5fEvstaMO3qxVGfc6oQnGgwczX2az/uHJ+r05H5WeAC/ackp5XmbbGvVGFvDabq0e F6oQ==; darn=zsh.org
- Arc-seal: i=1; a=rsa-sha256; t=1780828625; cv=none; d=google.com; s=arc-20240605; b=e1pZWbjQa4oRqrb8RDmXw+y4SRjueTvg9szZH2GfMCkLKP76zxvjtKC4o6kJSUKo2v sANSd/jxervL4jYuDH2w2hjS1RO7Upz3f5EVcJlPEW96dCeIIV7/2JOi/ehQShQTkWPt 1G8Ai6QWS0WhyHDlH/DunRXFBx+WlRl87Aqjvm04xvARLRsxPBHu1ALuJVQZcAyshE+M pUurKMCxvFBZ+0CkJWUbJ86VuM+0fvEiA8GPAJMO4+lHufxbFu3vTuSytikecrwLd92K aOYJVToBNqEeElWMvQWXJsw1H+aKfxZtxqmLYMU5nXMt3furk/494SOvEGUsMhU3IKrI eZtQ==
- Archived-at: <https://zsh.org/workers/54698>
- In-reply-to: <aiUvWWVf9MYQPa4m@chazelas.org>
- List-id: <zsh-workers.zsh.org>
- References: <20191217111113.z242f4g6sx7xdwru@chaz.gmail.com> <2ea6feb3-a686-4d83-ab27-6a582424487c@www.fastmail.com> <20200101140343.qwfx2xaojumuds3d@chaz.gmail.com> <20210430061117.buyhdhky5crqjrf2@chazelas.org> <CAH+w=7bHxSbFr60ZU0+oZ6+qEejhfBYTzvL7=aXadY5XzWtSzw@mail.gmail.com> <20210505114521.bemoiekpophssbug@chazelas.org> <20240308153050.u63fqtcjyr2yewye@chazelas.org> <20240309130310.5zovit5jk6l4rnak@chazelas.org> <20240310195201.c53tzhgcyk5qgi27@chazelas.org> <CAHYJk3QBHeR9TTGtpRu4SapMAWo9OC-YQ71cO8aSDcuJ8QEp_Q@mail.gmail.com> <aiUvWWVf9MYQPa4m@chazelas.org>
On Sun, Jun 7, 2026 at 10:49 AM Stephane Chazelas <stephane@xxxxxxxxxxxx> wrote:
>
> 2026-06-06 23:12:45 +0200, Mikael Magnusson:
> > Is this v6 version still applicable?
> [...]
>
> Should be, except I see it reverts the previous typo fix commit.
>
> The one below addresses it (and only that). I've done a few
> basic tests with and without rematchpcre on scalars and
> array/assoc elements and it seemed to work fine.
I'll commit it with this on top unless someone complains, setopt
functionargzero does not take effect when we have already entered the
function, so use $funcstack[1] instead. The comments in the script use
[ ] for replace and it appears to work, so I documented that. Also
noted that it internally reserves _regexp_replace_* parameter names.
diff --git i/Doc/Zsh/contrib.yo w/Doc/Zsh/contrib.yo
index eb52c1fe72..d25de2db52 100644
--- i/Doc/Zsh/contrib.yo
+++ w/Doc/Zsh/contrib.yo
@@ -4406,7 +4406,7 @@ the context prefix `tt(:completion:nslookup)'.
See also the tt(pager), tt(prompt) and tt(rprompt) styles below.
)
findex(regexp-replace)
-item(tt(regexp-replace) var(var) var(regexp) var(replace))(
+item(tt(regexp-replace) var(var) var(regexp) [ var(replace) ])(
Use regular expressions to perform a global search and replace operation
on a variable. POSIX extended regular expressions (ERE) are used,
unless the option tt(RE_MATCH_PCRE) has been set, in which case
@@ -4416,7 +4416,8 @@ library).
var(var) is the name of the variable containing the string to be matched.
The variable will be modified directly by the function. The
-variables tt(MATCH), tt(MBEGIN), tt(MEND), tt(match), tt(mbegin), tt(mend)
+variables tt(MATCH), tt(MBEGIN), tt(MEND), tt(match), tt(mbegin), tt(mend),
+as well as any variables starting with tt(_regexp_replace)
should be avoided as these are used by the regular expression code.
var(regexp) is the regular expression to match against the string.
@@ -4424,6 +4425,7 @@ var(regexp) is the regular expression to match
against the string.
var(replace) is the replacement text. This can contain parameter, command
and arithmetic expressions which will be replaced: in particular, a
reference to tt($MATCH) will be replaced by the text matched by the pattern.
+If not given, matches are replaced with nothing.
The return status is 0 if at least one match was performed, else 1.
diff --git i/Functions/Misc/regexp-replace w/Functions/Misc/regexp-replace
index c3c0c4a48a..1e44f65c05 100644
--- i/Functions/Misc/regexp-replace
+++ w/Functions/Misc/regexp-replace
@@ -15,8 +15,7 @@
# expression. Parsing errors are fatal to the shell process.
if (( $# < 2 || $# > 3 )); then
- setopt localoptions functionargzero
- print -ru2 "Usage: $0 <varname> <regexp> [<replacement>]"
+ print -ru2 "Usage: $funcstack[1] <varname> <regexp> [<replacement>]"
return 2
fi
--
Mikael Magnusson
Messages sorted by:
Reverse Date,
Date,
Thread,
Author