[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Matching sequences of identical characters with lpeg.
- From: Gabriel Bertilson <arboreous.philologist@...>
- Date: Tue, 20 Nov 2018 17:00:24 -0600
Here's a solution using Cmt. As Andrew pointed out, Cb just inserts a
capture into the list of captures returned by the current pattern, it
doesn't match anything.
Cg(1, "char") * Cmt(C(1) * Cb'char', function (_, _, char1, char2)
return char1 == char2 end)^0
It matches one character, labels it as "char", then matches further
characters if they are equal to "char". To use it on "aaabbbcccd" in
the Lua interpreter (with LPeg functions available as variables):
> patt = Cg(1, "char") * Cmt(C(1) * Cb'char', function (_, _, cur, prev) return cur == prev end)^0
> (C(patt)^1):match "aaabbbcccd"
aa bbb ccc d
On Tue, Nov 20, 2018 at 4:17 PM Magicks M <email@example.com> wrote:
> Hi, I'm having some trouble with this.
> I want to capture seqences on identical characters in a string: "aaabbbcccd" -> "aaa" "bbb" "ccc" "d"
> I attempted to use the regular pattern "((.)%1*)" and found that quantifiers didn't work. I then switched to lpeg and attempted to use:
> Cg(P(1), "char") * Cb'char'^0
> Which errors, and thus I cannot think of a good way to match this.