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

Re: Parameter Expansion pattern compatibility



On Wed, Dec 10, 2008 at 03:44:11PM +0100, Jerry Rocteur wrote:
> Hi,
> 
> I have one that I can sovle by using perl inside my zsh script but if anyone has time, I'm curious as to why it fails
> on zsh..
> 
> echo $ZSH_VERSION
> 4.2.6
> 
> Given a list of strings like this
> 
> set -A userId  p0btu pabtu p11btu p11eb41 rs0bt2 jro dri
> 
> I would like to see btu btu btu eb41 bt2 jro dri
> 
> if string begins with any case prt and is followed by one and one only [0-9] or [a-z] folowed by any mumber of digits.
> Remove everything up to the last digit.
> 
> In ksh we did it like this ${userId[$i]##([pPRrTt]@([0-9]|[a-z])*([0-9]))}

That's ksh globbing syntax (except for the outer (...)). To
enable it in zsh, you need setopt kshglob.

The zsh equivalent of @(...) is (...), the equivalent of *(x) is
x# (with extendedglob).

~$ setopt kshglob; echo ${userId##[pPRrTt]@([0-9]|[a-z])*([0-9])}
btu btu btu eb41 bt2 jro dri

~$ setopt nokshglob extendedglob; echo ${userId##[pPRrTt]([0-9]|[a-z])[0-9]#}
btu btu btu eb41 bt2 jro dri

-- 
Stéphane



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