* Matching beginning and end of word
@ 2015-10-08 7:51 Sebastian Gniazdowski
2015-10-08 9:23 ` Peter Stephenson
0 siblings, 1 reply; 7+ messages in thread
From: Sebastian Gniazdowski @ 2015-10-08 7:51 UTC (permalink / raw)
To: zsh-users
Hello,
in vim \< and \> match agains beginning and end of word. They work
like ^ and $ but for words, not lines. How to obtain this in Zsh? So
that in string like "abc_cd" the "cd" would not be matched, and in
"abc cd" only "cd" would be matched.
Best regards,
Sebastian Gniazdowski
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Matching beginning and end of word
2015-10-08 7:51 Matching beginning and end of word Sebastian Gniazdowski
@ 2015-10-08 9:23 ` Peter Stephenson
2015-10-08 16:46 ` Bart Schaefer
0 siblings, 1 reply; 7+ messages in thread
From: Peter Stephenson @ 2015-10-08 9:23 UTC (permalink / raw)
To: zsh-users
On Thu, 8 Oct 2015 09:51:44 +0200
Sebastian Gniazdowski <sgniazdowski@gmail.com> wrote:
> in vim \< and \> match agains beginning and end of word. They work
> like ^ and $ but for words, not lines. How to obtain this in Zsh? So
> that in string like "abc_cd" the "cd" would not be matched, and in
> "abc cd" only "cd" would be matched.
So by "beginning of word", you mean "something that is either not
preceded by anything, or is preceded by a non-word character"? (And
similarly for end.)
We do have the special chracter range [[:WORD:]]. So I think you need
to check both possibilities, using the (#s) for start (or (#e) for end)
that Bart pointed out yesterday.
% [[ "foo bar" = *([^[:WORD:]]|(#s))bar* ]] && print yes
yes
% [[ "bar foo" = *([^[:WORD:]]|(#s))bar* ]] && print yes
yes
% [[ "obar foo" = *([^[:WORD:]]|(#s))bar* ]] || print no
no
Note [[:WORD:]] is a moving target, as determined by $WORDCHARS. See
manzshexpn.
End of word left as an exercise.
pws
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Matching beginning and end of word
2015-10-08 9:23 ` Peter Stephenson
@ 2015-10-08 16:46 ` Bart Schaefer
2015-10-08 17:44 ` not an error? Ray Andrews
0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2015-10-08 16:46 UTC (permalink / raw)
To: zsh-users
On Oct 8, 10:23am, Peter Stephenson wrote:
}
} We do have the special chracter range [[:WORD:]]. So I think you need
} to check both possibilities, using the (#s) for start (or (#e) for end)
} that Bart pointed out yesterday.
The problem is that [^[:WORD:]] matches one character, whereas \< \>
match an empty string in between two characters. It's difficult if
not impossible construct a generic pattern for that. Whether you can
achieve the desired effect depends on the context; Peter's answer is
fine if you're simply comparing, but if an s/// repacement is desired
you have to be more careful.
^ permalink raw reply [flat|nested] 7+ messages in thread
* not an error?
2015-10-08 16:46 ` Bart Schaefer
@ 2015-10-08 17:44 ` Ray Andrews
2015-10-08 19:22 ` Bart Schaefer
0 siblings, 1 reply; 7+ messages in thread
From: Ray Andrews @ 2015-10-08 17:44 UTC (permalink / raw)
To: zsh-users
print -rl "${(@k)aliases}}"
Shouldn't the parser complain about the extra '}'?
OTOH I can see that in a 'left to right' style of parsing,
their's little? chance of actual confusion so perhaps as
a matter of policy it's decided to let it slide. It
certainly complains about too many opening '{', tho.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: not an error?
2015-10-08 17:44 ` not an error? Ray Andrews
@ 2015-10-08 19:22 ` Bart Schaefer
2015-10-08 19:56 ` Ray Andrews
0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2015-10-08 19:22 UTC (permalink / raw)
To: zsh-users
On Oct 8, 10:44am, Ray Andrews wrote:
}
} print -rl "${(@k)aliases}}"
}
} Shouldn't the parser complain about the extra '}'?
Why would it? You've put it in double quotes, so this is the same
situation as
print -rl "}"
} certainly complains about too many opening '{', tho.
Consider
print -rl "{${(@k)aliases}}"
print -rl "${(@k)aliases}{}"
print -rl "${(@k)aliases}}{"
Given the quotes, the only reason too many open braces is a problem is
when you are inside the syntax of ${...} where braces are significant.
As soon as a matching close-brace is found, double-quote context is
active again and braces cease to have meaning.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: not an error?
2015-10-08 19:22 ` Bart Schaefer
@ 2015-10-08 19:56 ` Ray Andrews
2015-10-08 20:14 ` Bart Schaefer
0 siblings, 1 reply; 7+ messages in thread
From: Ray Andrews @ 2015-10-08 19:56 UTC (permalink / raw)
To: zsh-users
On 10/08/2015 12:22 PM, Bart Schaefer wrote:
> Given the quotes, the only reason too many open braces is a problem is
> when you are inside the syntax of ${...} where braces are significant.
> As soon as a matching close-brace is found, double-quote context is
> active again and braces cease to have meaning.
>
Right. Yeah, I know about the special nature of the ${} syntax, but as
you say, once
a closing brace is found, the following brace is just a character within
the quotes.
It bugs me how long it's taking me to stop thinking in C. Mind ... do
you ever really
need the outer quotes around an array like that? Without the quotes I
get my
error msg as I'd intuitively expect it, and it seems now that it's the
quotes that
are counterintuitive because ${} obviates them, no? In this case it
seems only
to hide the '}' error, still ... with the 'quote everything' rule I end
up hardly
even seeing quotes anymore and it gets lazy, I'd like to use them to effect,
not just as a matter of course.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: not an error?
2015-10-08 19:56 ` Ray Andrews
@ 2015-10-08 20:14 ` Bart Schaefer
0 siblings, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2015-10-08 20:14 UTC (permalink / raw)
To: zsh-users
On Oct 8, 12:56pm, Ray Andrews wrote:
}
} ... do you ever really need the outer quotes around an array like that?
As usual it depends on the context. Zsh in "native" emulation handles
array expansions differently than other shells, so most of the time you
don't need quotes -- but if you want to train yourself to write shell
code in a way that's portable to other shells, it's better to get used
to quoting.
On the other hand if you're using ${(k)...} then that's already not
portable.
There are cases (mostly involving nested expansions) where you need the
quotes to get the right interpretation.
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2015-10-08 20:21 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-10-08 7:51 Matching beginning and end of word Sebastian Gniazdowski
2015-10-08 9:23 ` Peter Stephenson
2015-10-08 16:46 ` Bart Schaefer
2015-10-08 17:44 ` not an error? Ray Andrews
2015-10-08 19:22 ` Bart Schaefer
2015-10-08 19:56 ` Ray Andrews
2015-10-08 20:14 ` 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).