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

Re: parameter expansion or regex question



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