zsh-users
 help / color / mirror / code / Atom feed
From: Andy Kluger <andykluger@gmail.com>
To: zsh-users@zsh.org
Subject: Help: f (repeat) modifier, and histsubstpattern + :s/l/r/ modifier escaping of [{()}]
Date: Wed, 16 Aug 2023 14:32:51 -0400	[thread overview]
Message-ID: <CAO4D9E=+O-=66w0um7OzENVEiV11QfVUWYSTocacUX4rhYr1=Q@mail.gmail.com> (raw)

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


             reply	other threads:[~2023-08-16 18:33 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-16 18:32 Andy Kluger [this message]
2023-08-17  3:19 ` Bart Schaefer
2023-08-17 23:09   ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAO4D9E=+O-=66w0um7OzENVEiV11QfVUWYSTocacUX4rhYr1=Q@mail.gmail.com' \
    --to=andykluger@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).