Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Issue with ${var#(*_)(#cN,M)}

On Oct 19, 10:33am, Stephane Chazelas wrote:
} Subject: Issue with ${var#(*_)(#cN,M)}
} Unless I'm missing something, this looks like a bug:

Hm.  I think it's counting the number of times it backtracked.  E.g.

} ~$ a='1_2_3_4_5_6'
} ~$ echo ${a#(*_)(#c2)}
} 2_3_4_5_6

Here, it first matched "1_2_3_4_5_" but then couldn't match a second
time, so it backtracked, matched "1_", and stopped counting.

However, there's an interaction with ${a#...} here -- because you've
asked for the shortest match, glob.c:igetmatch() first tries for the
longest match and then "brute-force" (see comment in glob.c) looks
for a shorter one.  So the pattern code gets invoked multiple times.
To make (*_)(#c2) work as you'd expect (each (*_) uses the shortest
match and then is tried again on the remainder) I think we would have
to teach the pattern code itself about shortest/longest match.

There's a further issue that backreferences don't seem to be well-
defined when a parenthesized subpattern is required to repeat.

Messages sorted by: Reverse Date, Date, Thread, Author