ZSH wildcard expression limiting repetition support

regular expressionwildcardszsh

Does anyone know if there is a way to incorporate repetition constraints into ZSH wildcard expressions?

For example, to match all files starting with "ABC" following by one or more numbers, using grep one could do:

ls | grep -e "ABC[0-9]\+"

Is there any way to do this directly with ZSH glob strings? Something along the lines of:

ls "ABC[0-9]\+"

I've looked through the docs for ZSH and Googled for something like this, but so far have not found any such support.

Anyone know if this is possible?

Best Answer

Yes, use ## to match one or more occurrence of [0-9] like:

ABC[0-9]##

This requires extendedglob to be set, which is by default. If unset, set it first:

setopt extendedglob

Example:

% print -l ABC*
ABC
ABC75475
ABC8
ABC90

% print -l ABC[0-9]##
ABC75475
ABC8
ABC90
Related Question