Trying to solve a code-golf problem using zsh
, but my regex isn't working correctly.
Requirement
Given input string $1
, delete all spaces immediately to the left of any !
character.
Examples:
Input ($1) Expected Result:
" ! a! !b c!d !" => "! a!!b c!d!"
"a !" => "a!"
"! a" => "! a"
"! ! !" => "!!!"
"! " => "! "
I want a solution using zsh
builtins only, this was the closest I could get:
<<<${(S)1// *!/!}
Unfortunately --try it online-- this results in
!!!b!d!
! a
!!!
!
As you can see, the first line has been mangled too enthusiastically by the *
match. The zsh documentation (sec 5.9.2 & 5.9.3 of the Guide) is rather confusing on this point.
the +
precedence operator also doesn't work :( <<<${(S)1//+( )!/!}
setopt kshglob
in my answer, for 35 bytes. Not gonna win but it was good to learn :) – roblogic Sep 13 '19 at 17:45