lua-users home
lua-l archive

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


2013/5/22 marbux <marbux@gmail.com>:
> 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:

a1/b1

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

a1+1

Repeat indefinitely:

a1^0

Do the substitutions and leave the rest unchanged:

lpeg.Cs(P)

E.g.

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