${(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.
Links
Examples
- Non-greedy string replace
- Output:
1 2 3
value="Monday is a working day" echo Shortest match: ${(S)value//*day/...day} echo Longest match: ${value//*day/...day}
1 2
Shortest match: ...day...day Longest match: ...day
BashSupport Pro is a Zsh IDE with support for ${(S)…} – try it now!