Zsh -oextendedglob
, 82 69 51 bytes
<<<${1//(#m)(_?|[A-Z])/${${MATCH[2]:u}:-_$MATCH:l}}
Try it online!
Try it online!
Try it online!
Even if the flags count toward byte count, adding 14 bytes for -oextendedglob
still undercuts the 82 byte program!
<<<${1//(#m)(_?|[A-Z])/${${MATCH[2]:u}:-_$MATCH:l}}
${1// / } # replacement
(#m) # enable $MATCH parameter
(_?|[A_Z]) # "?" matches any character
${MATCH[2]:u} # substitute 2nd char, uppercased
${ :-_$MATCH:l} # if empty, substitute "_" and lowercased match
<<< # print to stdout