diff options
| author | Stephane Chazelas <stephane@chazelas.org> | 2026-06-07 09:49:01 +0100 |
|---|---|---|
| committer | Mikael Magnusson <mikachu@gmail.com> | 2026-06-10 05:20:52 +0200 |
| commit | c62c4a2861595378e9ea55a443ffe4bc5a47121e (patch) | |
| tree | 107d0482a056ced5dfddaf194ccf62ddaee6dd96 | |
| parent | 54707: fix autoload +X nonexist leaving funcdef at dummy (diff) | |
| download | zsh-c62c4a2861595378e9ea55a443ffe4bc5a47121e.tar zsh-c62c4a2861595378e9ea55a443ffe4bc5a47121e.tar.gz zsh-c62c4a2861595378e9ea55a443ffe4bc5a47121e.tar.bz2 zsh-c62c4a2861595378e9ea55a443ffe4bc5a47121e.tar.lz zsh-c62c4a2861595378e9ea55a443ffe4bc5a47121e.tar.xz zsh-c62c4a2861595378e9ea55a443ffe4bc5a47121e.tar.zst zsh-c62c4a2861595378e9ea55a443ffe4bc5a47121e.zip | |
54695: regexp-replace and ^, word boundary or look-behind operators (and more).
| -rw-r--r-- | ChangeLog | 4 | ||||
| -rw-r--r-- | Doc/Zsh/contrib.yo | 6 | ||||
| -rw-r--r-- | Functions/Misc/regexp-replace | 125 |
3 files changed, 74 insertions, 61 deletions
@@ -1,5 +1,9 @@ 2026-06-10 Mikael Magnusson <mikachu@gmail.com> + * Stephane Chazelas: 54695: Doc/Zsh/contrib.yo, + Functions/Misc/regexp-replace: regexp-replace and ^, word boundary + or look-behind operators (and more). + * 54707: Src/builtin.c: fix autoload +X nonexist leaving funcdef at dummy diff --git a/Doc/Zsh/contrib.yo b/Doc/Zsh/contrib.yo index eb52c1fe7..aeb47851c 100644 --- a/Doc/Zsh/contrib.yo +++ b/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 a/Functions/Misc/regexp-replace b/Functions/Misc/regexp-replace index d4408f0f7..86b28c5aa 100644 --- a/Functions/Misc/regexp-replace +++ b/Functions/Misc/regexp-replace @@ -1,91 +1,98 @@ -# Replace all occurrences of a regular expression in a variable. The -# variable is modified directly. Respects the setting of the -# option RE_MATCH_PCRE. +# Replace all occurrences of a regular expression in a scalar variable. +# The variable is modified directly. Respects the setting of the option +# RE_MATCH_PCRE, but otherwise sets the zsh emulation mode. # -# First argument: *name* (not contents) of variable. -# Second argument: regular expression -# Third argument: replacement string. This can contain all forms of -# $ and backtick substitutions; in particular, $MATCH will be replaced -# by the portion of the string matched by the regular expression. +# Arguments: +# +# 1. *name* (not contents) of variable or more generally any lvalue; +# expected to be scalar. +# +# 2. regular expression +# +# 3. replacement string. This can contain all forms of +# $ and backtick substitutions; in particular, $MATCH will be +# replaced by the portion of the string matched by the regular +# expression. Parsing errors are fatal to the shell process. -# we use positional parameters instead of variables to avoid -# clashing with the user's variable. Make sure we start with 3 and only -# 3 elements: -argv=("$1" "$2" "$3") +if (( $# < 2 || $# > 3 )); then + print -ru2 "Usage: $0 <varname> <regexp> [<replacement>]" + return 2 +fi -# $4 records whether pcre is enabled as that information would otherwise -# be lost after emulate -L zsh -4=0 -[[ -o re_match_pcre ]] && 4=1 +local _regexp_replace_use_pcre=0 +[[ -o re_match_pcre ]] && _regexp_replace_use_pcre=1 emulate -L zsh +local _regexp_replace_subject=${(P)1} \ + _regexp_replace_regexp=$2 \ + _regexp_replace_replacement=$3 \ + _regexp_replace_result \ + MATCH MBEGIN MEND -local MATCH MBEGIN MEND local -a match mbegin mend -if (( $4 )); then +if (( _regexp_replace_use_pcre )); then # if using pcre, we're using pcre_match and a running offset # That's needed for ^, \A, \b, and look-behind operators to work # properly. zmodload zsh/pcre || return 2 - pcre_compile -- "$2" && pcre_study || return 2 + pcre_compile -- "$_regexp_replace_regexp" && pcre_study || return 2 - # $4 is the current *byte* offset, $5, $6 reserved for later use - 4=0 6= + local _regexp_replace_offset=0 _regexp_replace_start _regexp_replace_stop _regexp_replace_new ZPCRE_OP + local -a _regexp_replace_finds - local ZPCRE_OP - while pcre_match -b -n $4 -- "${(P)1}"; do - # append offsets and computed replacement to the array - # we need to perform the evaluation in a scalar assignment so that if - # it generates an array, the elements are converted to string (by + while pcre_match -b -n $_regexp_replace_offset -- "$_regexp_replace_subject"; do + # we need to perform the evaluation in a scalar assignment so that + # if it generates an array, the elements are converted to string (by # joining with the first character of $IFS as usual) - 5=${(e)3} - argv+=(${(s: :)ZPCRE_OP} "$5") + _regexp_replace_new=${(Xe)_regexp_replace_replacement} + + _regexp_replace_finds+=( ${(s[ ])ZPCRE_OP} "$_regexp_replace_new" ) # for 0-width matches, increase offset by 1 to avoid # infinite loop - 4=$((argv[-2] + (argv[-3] == argv[-2]))) + (( _regexp_replace_offset = _regexp_replace_finds[-2] + (_regexp_replace_finds[-3] == _regexp_replace_finds[-2]) )) done - (($# > 6)) || return # no match + (( $#_regexp_replace_finds )) || return # no match - set +o multibyte + unsetopt multibyte - # $5 contains the result, $6 the current offset - 5= 6=1 - for 2 3 4 in "$@[7,-1]"; do - 5+=${(P)1[$6,$2]}$4 - 6=$(($3 + 1)) + _regexp_replace_offset=1 + for _regexp_replace_start _regexp_replace_stop _regexp_replace_new in "$_regexp_replace_finds[@]"; do + _regexp_replace_result+=${_regexp_replace_subject[_regexp_replace_offset,_regexp_replace_start]}$_regexp_replace_new + (( _regexp_replace_offset = _regexp_replace_stop + 1 )) done - 5+=${(P)1[$6,-1]} -else + _regexp_replace_result+=${_regexp_replace_subject[_regexp_replace_offset,-1]} + +else # no PCRE # in ERE, we can't use an offset so ^, (and \<, \b, \B, [[:<:]] where # available) won't work properly. - # $4 is the string to be matched - 4=${(P)1} - - while [[ -n $4 ]]; do - if [[ $4 =~ $2 ]]; then - # append initial part and substituted match - 5+=${4[1,MBEGIN-1]}${(e)3} - # truncate remaining string - if ((MEND < MBEGIN)); then - # zero-width match, skip one character for the next match - ((MEND++)) - 5+=${4[1]} - fi - 4=${4[MEND+1,-1]} - # indicate we did something - 6=1 - else - break + local _regexp_replace_ok=0 + while [[ $_regexp_replace_subject =~ $_regexp_replace_regexp ]]; do + # append initial part and substituted match + _regexp_replace_result+=$_regexp_replace_subject[1,MBEGIN-1]${(Xe)_regexp_replace_replacement} + # truncate remaining string + if (( MEND < MBEGIN )); then + # zero-width match, skip one character for the next match + (( MEND++ )) + _regexp_replace_result+=$_regexp_replace_subject[MBEGIN] fi + _regexp_replace_subject=$_regexp_replace_subject[MEND+1,-1] + _regexp_replace_ok=1 + [[ -z $_regexp_replace_subject ]] && break done - [[ -n $6 ]] || return # no match - 5+=$4 + (( _regexp_replace_ok )) || return + _regexp_replace_result+=$_regexp_replace_subject fi -eval $1=\$5 +# assign result to target variable if at least one substitution was +# made. At this point, if the variable was originally array or assoc, it +# is converted to scalar. If $1 doesn't contain a valid lvalue +# specification, an exception is raised (exits the shell process if +# non-interactive). +: ${(P)1::="$_regexp_replace_result"} + |
