lua-users home
lua-l archive

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




25.07.2015 18:00, lua-l-request@lists.lua.org пишет:
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?
> 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
I did it with string capture:

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

Alexander Mashin