lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Tim Mensch <> writes:

> Something I noticed as I was reading the pattern docs for my post is
> that Lua adds one thing to patterns that I've not seen in regular
> expressions that does allow you to parse nested brackets:
> ----
> %bxy, where x and y are two distinct characters; such item matches
> strings that start with x, end with y, and where the x and y are
> balanced. This means that, if one reads the string from left to right,
> counting +1 for an x and -1 for a y, the ending y is the first y where
> the count reaches 0. For instance, the item %b() matches expressions
> with balanced parentheses.
> ----
> I hadn't run across that before, and it's cool.

It is not cool enough since it does not help matching stuff with escape
sequences or similar in it.

One would rather need:

%+n matches the empty string and increases counter n
%-n matches the empty string if counter n is non-zero, decreasing it
%=n matches the empty string if counter n is zero

Another possibility would be to have %rn where %rn substitutes the
definition of %n recursively.

David Kastrup