LPeg: inserting previous captures by substitution

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

LPeg: inserting previous captures by substitution

Александр Машин
Dear All,

I wrote a simple LPeg grammar in re.lua syntax that matches a signless
integer with digits grouped by spaces (there are several ways to do it
in different cultures):

int <- digits spaces int / digits
digits <- [0-9]+
spaces <- %s+

It works, matching figures like "6 000 000".

I can also remove the spaces with substitution capture:

int <- {~ digits spaces -> '' int / digits ~}
digits <- [0-9]+
spaces <- %s+

It will match "6 000 000" and return "6000000".

But what if I want to return "6000000|6 000 000"? Can I memorise the
original capture, before removing the spaces and insert it by
substitution after processed string? I mean, can I do it with pure re
syntax, without function captures?

Thanks in advance,

Alexander Mashin

Reply | Threaded
Open this post in threaded view
|

Re: LPeg: inserting previous captures by substitution

Patrick Donnelly
On Fri, Jul 24, 2015 at 2:45 PM, Александр Машин <[hidden email]> wrote:
> But what if I want to return "6000000|6 000 000"? Can I memorise the
> original capture, before removing the spaces and insert it by substitution
> after processed string? I mean, can I do it with pure re syntax, without
> function captures?

Use this:

integer <- { int }
int <- {~ digits spaces -> '' int / digits ~}

That gives you two captures, the first is the unmodified (unsubstituted) match:

print(patt:match("6 000 000")) -->  6 000 000       6000000

--
Patrick Donnelly