Re: capturing in ${VAR//(*(\/)(+([^\/]))?(\/)/_${BASH_REMATCH[1]}_}
On Mon, Feb 8, 2016 at 9:20 AM, Stephane Chazelas 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
Re: capturing in ${VAR//(*(\/)(+([^\/]))?(\/)/_${BASH_REMATCH[1]}_}
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
Re: capturing in ${VAR//(*(\/)(+([^\/]))?(\/)/_${BASH_REMATCH[1]}_}
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. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/
capturing in ${VAR//(*(\/)(+([^\/]))?(\/)/_${BASH_REMATCH[1]}_}
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? Couldn't find anything in the manpage, but I could easily have missed something...? Thanks...