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

Re: Matching beginning and end of word



On Thu, 8 Oct 2015 09:51:44 +0200
Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx> 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



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