${(S)…}: Closest and non-greedy matching

(S) is only useful with parameter expansion operators ${...#...}, ${...%...}, and ${.../...}.

${(S)...#...}
${(S)...##...}
Search for the match that starts closest to the start of the string
${(S)...%...}
${(S)...%%...}
Search for the match that starts closest to the end of the string
${(S).../...}
${(S)...//...}
Non-greedy matching to replace the shortest instead of the longest match.

Examples

Non-greedy string replace
1
2
3
value="Monday is a working day"
echo Shortest match: ${(S)value//*day/...day}
echo Longest match: ${value//*day/...day}
Output:
1
2
Shortest match: ...day...day
Longest match: ...day
BashSupport Pro 是一款支持 S** 的 **Zsh 集成开发环境 - 立即试用
© 2020–2024 Joachim Ansorg
声明
隐私政策
许可条款