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

Re: backreferences



On Oct 15, 10:36pm, Ray Andrews wrote:
}
} > 	if [[ "$sstring" = (#b)((^(edcba))*)(edcba)(*) ]]
} >
} > There are 5 sets of parens, and you care about $match[1], $match[4],
} > and $match[5].  $match[2] is the prefix of $match[1] that was not
} > consumed by the middle *, and $match[3] is an empty substring of
} > $match[2] (because it was excluded from matching).  Count off the
} > open parens left to right to see this.
} At the moment I'm quite confounded.  So '(b#)' is looking at every set 
} of '()' even
} when nested and even when 'doing something else'?

Yes.

} > In fact you don't even need the middle * because (^edcba) will eat
} > an arbitrarily long string as long as it is not literally "edcba".
} Well, that's the original question.

As noted in my follow-up mail, what I wrote there is actually not right;
the part after "because" is correct, but the part about not needing the
middle * is wrong, because (^edcba) matches xxxxedcbaxxxx just fine, and
I assume you don't want that.

} > So you can reduce this to
} >
} > 	if [[ "$sstring" = (#b)(^edcba)(edcba)(*) ]]

This needs to be (#b)(^edcba*)(edcba)(*)

} God knows.  But your simplified command works fine too, and I'll
} take it on faith.  I've never seen any sort of 'any number of characters'
} sort  of thing look other than:
} [....]*

No, now you're confusing grep-style regular expressions with zsh patterns.
    EGREP	ZSH
    .		?
    .*		* or ?#
    .+		?##
    .?		(?|)
    [xyz]	[xyz]
    [xyz]*	[xyz]#

There's a lot more but those are the most important bits.

} ... so you can see where I'd go astray there.  Ok, so
} ^(edcba)
} is individual character matches and
} (^edcba)
} is  anything up to "edcba"

No.  [^edcba] is individual character matches and (^edcba) is anything
other than the literal string edcba, including longer strings that have
edcba as a substring.  Negated patterns are really tricky.



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