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).



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