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

Re: parameter expansion or regex question



Hi Bart,

thanks so much for your reply.

> setopt extendedglob
> CLEANFILENAME="${FILENAME/((#s)|.)q<1-5>(.|(#e))/}"

> setopt extendedglob
> CLEANFILENAME="${FILENAME/(#b)(#s)(|*).##q<1-5>.#(*|)(#e)/$match[1]$match[2]}"

Great!  Hm, but both forget a dot if the q? is in the middle,
e.g. foo.bar.q1.baz leads to foo.barbaz

But no problem, cause I really like this one:
> CLEANFILENAME="${(j:.:)${(@)${(@s:.:)FILENAME}:#q<1-5>}}"
> 
> 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.

I actually like the sideeffect you mentioned concerning multiple
consecutive dots because it cleans up a "bad" filename.
Just out of curiosity, what adjustments would have to be made so that empty
array elements aren't ignored and thus multiple dots are kept? 

> 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}"
Thanks for even correcting my solution - really interesting!
Ah, and now I even understand why your spelled-out version doesn't work!
You forgot the ! before the if-condition. :-)

Thanks again!
 Andy.

-- 
                              o      _     _         _
  ------- __o       __o      /\_   _ \\o  (_)\__/o  (_)          -o)
  ----- _`\<,_    _`\<,_    _>(_) (_)/<_    \_| \   _|/' \/       /\\
  ---- (_)/ (_)  (_)/ (_)  (_)        (_)   (_)    (_)'  _\o_    _\_v
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 The more things change, the more they remain the same.



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