zsh-users
 help / color / mirror / code / Atom feed
* Help: f (repeat) modifier, and histsubstpattern + :s/l/r/ modifier escaping of [{()}]
@ 2023-08-16 18:32 Andy Kluger
  2023-08-17  3:19 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Andy Kluger @ 2023-08-16 18:32 UTC (permalink / raw)
  To: zsh-users

Hello!

For a small exercise I wanted to repeatedly remove empty pairs of
brackets/braces/parentheses from a string, and started with this
working solution:

    s=$1 l=$#s
    while (( 1 )) {
      s=${s//(\{\}|\(\)|\[\])}
      if [[ $#s < $l ]] { l=$#s } else { break }
    }
    print -r -- $s

But I remembered reading about the f (repeat) modifier, and thought
I'd try to remove the while loop.

    setopt histsubstpattern
    s='left[()]over'
    print -rl -- ${s:fs/(\\{\\}|\\(\\)|\\[\\])//}
    # printed: left[]over
    # expected: leftover

I'm having two confusions:

1. Why do I need to double-backslash before each
brace/bracket/parenthesis, rather than single-backslash?
2. Why doesn't the replacement seem to happen to each subsequent
result of a former replacement?

Thanks for any help!

Andy


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-08-17 23:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-16 18:32 Help: f (repeat) modifier, and histsubstpattern + :s/l/r/ modifier escaping of [{()}] Andy Kluger
2023-08-17  3:19 ` Bart Schaefer
2023-08-17 23:09   ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).