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

Re: Paramater subsiution



On Sun, Jun 27, 2021, at 6:47 PM, Gamma wrote:
> echo ${i##* - }
> 
> Removes the longest prefix ending with ' - '.

${i#* - } would also work and would permit "name" to be completely
arbitrary (assuming that "file 1234" could not possibly contain
" - " as a substring).

https://zsh.sourceforge.io/Doc/Release/Expansion.html#Parameter-Expansion

On Sun, Jun 27, 2021, at 6:40 PM, Lewis Butler wrote:
> I've trued using
> 
> ${i:s/file \[0-9\]+ - //}
> ${i:s/file \[0-9\]\[0-9\]\[0-9\]\[0-9\] - //}
> ${i:s/file [0-9]+ - //}
> ${i:s/file [0-9][0-9][0-9][0-9] - //}
> ${i:s/file [^ ]+ - //}

First, you probably wanted ${i/foo/bar} rather than ${i:s/foo/bar}.
The latter matches foo literally unless the HIST_SUBST_PATTERN
option is set.  It's also less appropriate in a conceptual sense.

Second, these substitution features use patterns ("globs"), not
regular expressions.  Bracket expressions are treated specially
([...], not \[...\]), but '+' generally is not.  You can find details
here, including how you could achieve the same effect as '+' (if
you really must):

https://zsh.sourceforge.io/Doc/Release/Expansion.html#Filename-Generation

But you should use Gamma's solution (or my variant), which is simple
and highly portable.

-- 
vq




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