Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: parameter expansion or regex question
- X-seq: zsh-users 8514
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Andy Spiegl <zsh.Andy@xxxxxxxxx>, ZSH User List <zsh-users@xxxxxxxxxx>
- Subject: Re: parameter expansion or regex question
- Date: Thu, 17 Feb 2005 05:41:16 +0000
- In-reply-to: <20050217030355.GA2943@xxxxxxxxx>
- Mailing-list: contact zsh-users-help@xxxxxxxxxx; run by ezmlm
- References: <20050217030355.GA2943@xxxxxxxxx>
On Feb 17, 4:03am, Andy Spiegl wrote:
}
} In Perl I'd do it similar to this:
} FILENAME =~ /(^|.)q[1-5](.|$)/;
} But I have no idea how/whether zsh can handle regexps.
The closest approximation to (the corrected form of) that perl is:
setopt extendedglob
CLEANFILENAME="${FILENAME/((#s)|.)q<1-5>(.|(#e))/}"
Another regex-like way would be:
setopt extendedglob
CLEANFILENAME="${FILENAME/(#b)(#s)(|*).##q<1-5>.#(*|)(#e)/$match[1]$match[2]}"
But you could also use (no extendedglob needed):
CLEANFILENAME="${(j:.:)${(@)${(@s:.:)FILENAME}:#q<1-5>}}"
The latter two solutions need adjustment if the file name may contain
multiple consecutive dots.
The last splits the file into an array at dots, discards all elements
that look like q<1-5>, then joins the remainder back together again.
The way you were trying to do it:
prefix="${${${(M)FILENAME#*q<1-5>.}:-${FILENAME%.q<1-5>*}}%q<1-5>.}"
suffix="${${${(M)FILENAME%.q<1-5>*}:-${FILENAME#*q<1-5>.}}#.q<1-5>}"
CLEANFILENAME="${prefix}${suffix#.}"
Spelled out:
prefix="${(M)FILENAME#*q<1-5>.}"
if [[ -n "$prefix" ]]; then prefix="${FILENAME%.q<1-5>*}"; fi
prefix="${prefix%q<1-5>.}"
suffix="${(M)FILENAME%.q<1-5>*}"
if [[ -n "$suffix" ]]; then suffix="${FILENAME#*q<1-5>.}"; fi
suffix="${suffix#.q<1-5>}"
suffix="${suffix#.}"
CLEANFILENAME="${prefix}${suffix}"
Messages sorted by:
Reverse Date,
Date,
Thread,
Author