* Possible to match string supporting backslash quoting?
@ 2023-01-27 11:47 Sebastian Gniazdowski
2023-01-27 13:30 ` Stephane Chazelas
0 siblings, 1 reply; 3+ messages in thread
From: Sebastian Gniazdowski @ 2023-01-27 11:47 UTC (permalink / raw)
To: Zsh Users
[-- Attachment #1: Type: text/plain, Size: 624 bytes --]
HI,
I'm matching var="…" and would want to support quoting, i.e.:
var="abc\"ef". So far I have:
qstr='(#B)(["](#b)(*~(*[^\\]([\\][\\])#["]*)))["]'
[[ '"qqq--\\"abc"' == (#b)$~qstr ]];printf ·ec=%s\\n $?;@str-dump match
Output:
·ec=1
qstr='(#B)(["](#b)(*~(*[^\\]([\\][\\])#["]*)))["]'
[[ '"qqq--\"abc"' == (#b)$~qstr ]];printf ·ec=%s\\n $?;@str-dump match
Output:
·ec=0
1: qqq--\"abc
2:
3:
So it at least detects a \\" in the input. Would it be possible to utilize
the negations ~ and ^ to somehow match "str\"ef" as: str"ef, and "str\\"ef"
as: str?
--
Best regards,
Sebastian Gniazdowski
[-- Attachment #2: Type: text/html, Size: 2080 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Possible to match string supporting backslash quoting?
2023-01-27 11:47 Possible to match string supporting backslash quoting? Sebastian Gniazdowski
@ 2023-01-27 13:30 ` Stephane Chazelas
2023-01-27 13:35 ` Stephane Chazelas
0 siblings, 1 reply; 3+ messages in thread
From: Stephane Chazelas @ 2023-01-27 13:30 UTC (permalink / raw)
To: Sebastian Gniazdowski; +Cc: Zsh Users
2023-01-27 11:47:14 +0000, Sebastian Gniazdowski:
> HI,
> I'm matching var="…" and would want to support quoting, i.e.:
> var="abc\"ef". So far I have:
>
> qstr='(#B)(["](#b)(*~(*[^\\]([\\][\\])#["]*)))["]'
> [[ '"qqq--\\"abc"' == (#b)$~qstr ]];printf ·ec=%s\\n $?;@str-dump match
> Output:
> ·ec=1
>
> qstr='(#B)(["](#b)(*~(*[^\\]([\\][\\])#["]*)))["]'
> [[ '"qqq--\"abc"' == (#b)$~qstr ]];printf ·ec=%s\\n $?;@str-dump match
>
> Output:
> ·ec=0
> 1: qqq--\"abc
> 2:
> 3:
>
> So it at least detects a \\" in the input. Would it be possible to utilize
> the negations ~ and ^ to somehow match "str\"ef" as: str"ef, and "str\\"ef"
> as: str?
[...]
I didn't try to decipher your patterns, but to match a quoted
strings while handling backslash escaping, in ERE, you do:
"(\\.|[^\\"])*"
Whose zsh -o extendedglob equivalent would be:
"(\\?|[^\\"])#"
--
Stephane
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Possible to match string supporting backslash quoting?
2023-01-27 13:30 ` Stephane Chazelas
@ 2023-01-27 13:35 ` Stephane Chazelas
0 siblings, 0 replies; 3+ messages in thread
From: Stephane Chazelas @ 2023-01-27 13:35 UTC (permalink / raw)
To: Sebastian Gniazdowski, Zsh Users
2023-01-27 13:30:03 +0000, Stephane Chazelas:
[...]
> to match a quoted
> string while handling backslash escaping, in ERE, you do:
>
> "(\\.|[^\\"])*"
>
> Whose zsh -o extendedglob equivalent would be:
>
> "(\\?|[^\\"])#"
[...]
For instance, to requote the double-quoted strings with single
quotes:
~$ text='"quoted\\" not "quoted\"quoted" not'
~$ pattern='"(\\?|[^\\"])#"'
~$ print -r -- ${text//(#m)$~pattern/${(qq)${(Q)MATCH}}}
'quoted\' not 'quoted"quoted' not
--
Stephane
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-01-27 13:37 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-27 11:47 Possible to match string supporting backslash quoting? Sebastian Gniazdowski
2023-01-27 13:30 ` Stephane Chazelas
2023-01-27 13:35 ` Stephane Chazelas
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).