* substitution groups and patterns i replace string (Emacs' \\1 etc.)
@ 2016-03-03 1:08 Emanuel Berg
2016-03-03 9:41 ` Peter Stephenson
0 siblings, 1 reply; 5+ messages in thread
From: Emanuel Berg @ 2016-03-03 1:08 UTC (permalink / raw)
To: zsh-users
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?
Or, can I feed the match to a function which I define
myself which returns the replace string? I think I can
bash it together tho it won't be as elegant...
I particular, I'm doing text-to-HTML conversion so
I want to search for http://... the URLs in the text.
To search for them is easy enough but how do I use the
result (the match string) to construct the
replacement, i.e. <a href="\\1"> ... </a> if, again,
the Emacs notation is used to illustrate?
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: substitution groups and patterns i replace string (Emacs' \\1 etc.)
2016-03-03 1:08 substitution groups and patterns i replace string (Emacs' \\1 etc.) Emanuel Berg
@ 2016-03-03 9:41 ` Peter Stephenson
2016-03-06 5:57 ` Emanuel Berg
0 siblings, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2016-03-03 9:41 UTC (permalink / raw)
To: zsh-users
On Thu, 3 Mar 2016 02:08:22 +0100
Emanuel Berg <embe8573@student.uu.se> wrote:
> 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.
local -a match mbegin mend
if [[ $url = (#b)http://(*) ]]; then
print $match[1]
fi
Works with other forms of pattern matching.
pws
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: substitution groups and patterns i replace string (Emacs' \\1 etc.)
2016-03-03 9:41 ` Peter Stephenson
@ 2016-03-06 5:57 ` Emanuel Berg
2016-03-06 7:15 ` Mikael Magnusson
0 siblings, 1 reply; 5+ messages in thread
From: Emanuel Berg @ 2016-03-06 5:57 UTC (permalink / raw)
To: zsh-users
Peter Stephenson <p.stephenson@samsung.com> 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
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.
Perhaps Emacs/sed syntax in this case is something
that can be added to zsh?
--
underground experts united
http://user.it.uu.se/~embe8573
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: substitution groups and patterns i replace string (Emacs' \\1 etc.)
2016-03-06 5:57 ` Emanuel Berg
@ 2016-03-06 7:15 ` Mikael Magnusson
2016-03-08 2:52 ` Bart Schaefer
0 siblings, 1 reply; 5+ messages in thread
From: Mikael Magnusson @ 2016-03-06 7:15 UTC (permalink / raw)
To: Zsh Users
On Sun, Mar 6, 2016 at 6:57 AM, Emanuel Berg <embe8573@student.uu.se> wrote:
> Peter Stephenson <p.stephenson@samsung.com> 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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: substitution groups and patterns i replace string (Emacs' \\1 etc.)
2016-03-06 7:15 ` Mikael Magnusson
@ 2016-03-08 2:52 ` Bart Schaefer
0 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2016-03-08 2:52 UTC (permalink / raw)
To: Zsh Users
On Mar 6, 8:15am, Mikael Magnusson wrote:
}
} % echo ${a//(#b)([aoeui])/${(U)match[1]}}
It's actually quite simple to make $1, $2, ... refer to the values in
the $match array during ${v//p/r} substitutions. This would make the
syntax look very perl-ish, but would mean you can't combine backrefs
with expansions of the normal positional parameters.
Anyone interested? Discuss?
Incidentally, for a fun time consider replacing ${(U)match[1]} with
${(P)match[1]} in that substitution of Mikael's.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2016-03-08 2:52 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-03 1:08 substitution groups and patterns i replace string (Emacs' \\1 etc.) Emanuel Berg
2016-03-03 9:41 ` Peter Stephenson
2016-03-06 5:57 ` Emanuel Berg
2016-03-06 7:15 ` Mikael Magnusson
2016-03-08 2:52 ` Bart Schaefer
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).