bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: capturing in ${VAR//(*(\/)(+([^\/]))?(\/)/_${BASH_REMATCH[1]}_}


From: Dan Douglas
Subject: Re: capturing in ${VAR//(*(\/)(+([^\/]))?(\/)/_${BASH_REMATCH[1]}_}
Date: Sat, 20 Feb 2016 03:30:52 -0600

On Mon, Feb 8, 2016 at 9:20 AM, Stephane Chazelas
<address@hidden> wrote:
> 2016-02-08 09:00:09 -0500, Chet Ramey:
>> On 2/8/16 2:47 AM, Linda Walsh wrote:
>> > When you are doing a var expansion using the
>> > replacement format ${VAR//./.}, is there some way to
>> > put parens around some part of the expression and reference
>> > them as in the [[V~re]] RE-matches?
>>
>> No.  Shell patterns do not have backreferences.
> [...]
>
> Note that the feature is available in other shells and quite
> handy there. It could be worth adding to bash
>
> $ zsh -o extendedglob -c 'a=1234; echo 
> ${a//(#b)(?)(?)/${match[2]}${match[1]}}'
> 2143
> (#b) to activate back-references stored in $match array.
>
> $ zsh -o extendedglob -c 'a=1234; echo ${a//(#m)?/<$MATCH>}'
> <1><2><3><4>
> (#m) to record the matched portion in $MATCH.
>
> Though I suspect for bash you would prefer the ksh93 syntax:
>
> $ ksh93 -c 'a=1234; echo ${a//@(?)@(?)/\2\1}'
> 2143

Technically that's "grouping", but yeah it's a useful feature. ksh
does backrefs in plain shell patterns also.

$ ksh -c 'a=11223344; echo "${a//@(@(?)\2)@(@(?)\4)/\3\1}"'
22114433



reply via email to

[Prev in Thread] Current Thread [Next in Thread]