lua-users home
lua-l archive

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

2013/5/22 marbux <>:
> My thanks to you all. I will test each of these methods and go with
> the one that seems the most foolproof, given other code in the script.
> A special thanks for noticing that the script also needs to escape
> magic characters, which I had not yet caught.

Since nobody has up to now supported Sean's suggestion of LPEG,
I'll do so now. I recently put in the required amount of activation energy
to get my LPEG competence going, and the reaction is now exothermic.
In non-chemical terms, there's a steep learning curve but once you
reach the plateau you just keep going effortlessly.

Building a pattern that tries several possibilities in a prescribed order
is one of the most basic LPEG constructs: a1+a2+a3+...+an

Prescribing the replacement is hardly less basic:


Saying "move up one character if there is no match" is easy too


Repeat indefinitely:


Do the substitutions and leave the rest unchanged:



    Cs, P = lpeg.Cs, lpeg.P
    pat = Cs((P"ABC"/"abc" + P"AB"/"de" + P"A"/"f"+P(1)/".")^0)