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: Stephane Chazelas
Subject: Re: capturing in ${VAR//(*(\/)(+([^\/]))?(\/)/_${BASH_REMATCH[1]}_}
Date: Mon, 8 Feb 2016 15:20:23 +0000
User-agent: Mutt/1.5.21 (2010-09-15)

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

-- 
Stephane



reply via email to

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