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

Re: Regexp type pattern matching



On Thu, May 08, 2008 at 11:09:39AM +0200, Jerry Rocteur wrote:
[...]
> >   if [[ $USER_NAME = [prt][0-9]* ]]; then
> >     ...
> >   fi
[...]
> you know I tried that but instead of = I put =~ and it did not work.

=~ works with recent versions of zsh and if you have the
zsh/pcre module, you can use perl-like regexps with setopt
re_match_pcre.

[[ $USER_NAME =~ ^[prt][0-9]* ]]

[[ string = pattern ]]

treats pattern as a zsh globbing shell pattern.

zsh patterns are equivalent to regexp (with additions) when the
extended-glob option is on, only with a different syntax.

Here is a quick table of correspondance:

RE      zsh
.	?
*	#
+	##
?	(<pattern>|)
|	|
()	()
^	implied
$	implied
[]	[]
(i:...) (#i)
...

> Thanks very much for this information I really appreciate it.
> 
> Jerry
> P.S. Can you please tell me which manual this [[ $USER_NAME =
> [prt][0-9]* ]] is explained, I looked this morning for a
> few hours and I didn't seen anything like this ?

If you type "info zsh", then "i", then "condi<Tab><Tab>", you'll
see:

3 completions:
condition, completion style    conditional expression
conditional expressions

Select "conditional expressions"

"i" is for "index". You can do the same with the table of
content with the "g" key. The index works better with version
4.12 and above of info.

You can also search the whole manual (with regexps with 4.12)
with the "s" key.

All the key bindings are fully configurable, so if like me, you
don't like the emacs-like key-binding, you can change it.

In any case, remember that what makes the power of "info" is "i"
and "g" (with <Tab>). With a well written manual like zsh, 99%
of the time, I get to the information I'm looking for in a few
keystrokes.

-- 
Stéphane



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