Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: substitution groups and patterns i replace string (Emacs' \\1 etc.)



On Sun, Mar 6, 2016 at 6:57 AM, Emanuel Berg <embe8573@xxxxxxxxxxxxx> wrote:
> Peter Stephenson <p.stephenson@xxxxxxxxxxx> writes:
>
>>> When I do substitution, can I do groups and
>>> patterns like in Emacs with \\1, \\2 to reference
>>> the particular matches, and thus construct the
>>> replace string out of them any way I like?
>>
>> You do it with $match, and if you need to refer to
>> positions in the original string, $mbegin and $mend.
>> See the description of the (#b) pattern in the
>> zshexpn manual page.
>
> I have now checked out the zshexpn(1) man page but the
> examples there are very complicated!
>
>> local -a match mbegin mend
>>
>> if [[ $url = (#b)http://(*) ]]; then print $match[1]
>> fi

% a="> Thanks, but this syntax is very bulky compared to
> Emacs and sed. How it is supposed to be used for
> a stream of text or a variable with several hit
> instances? I'm not saying it can't be done only
> compared to Emacs (and sed) it seems
> unnecessarily complicated."

% echo ${a//(#b)([aoeui])/${(U)match[1]}}
> ThAnks, bUt thIs syntAx Is vEry bUlky cOmpArEd tO
> EmAcs And sEd. HOw It Is sUppOsEd tO bE UsEd fOr
> A strEAm Of tExt Or A vArIAblE wIth sEvErAl hIt
> InstAncEs? I'm nOt sAyIng It cAn't bE dOnE Only
> cOmpArEd tO EmAcs (And sEd) It sEEms
> UnnEcEssArIly cOmplIcAtEd.


-- 
Mikael Magnusson



Messages sorted by: Reverse Date, Date, Thread, Author