[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Any LPEG tutorial for laymen ?
- From: Choonster TheMage <choonster.2010@...>
- Date: Wed, 25 Sep 2013 01:38:11 +1000
On 25 September 2013 00:50, Jayanth Acharya <jayachar88@gmail.com> wrote:
> Agreed... clearer, and somewhat more familiar (in standard posix regex
> sense). Thanks for clarifying @Craig.
>
> However, I am also trying to comprehend the comment by @Choonster:
>
>> Use `require("lpeg")` if you want to manually create all the patterns
>> or `require("re")` if you want to use a regular expression syntax
>> instead.
>
> If someone can share 2 small illustrative examples of when to use which one,
> or why -- it'd be excellent. I know that I am perhaps asking for too much.
>
>
>
> On Tue, Sep 24, 2013 at 7:35 PM, Craig Barnes <craigbarnes85@gmail.com>
> wrote:
>>
>> On 24 September 2013 14:29, Jayanth Acharya <jayachar88@gmail.com> wrote:
>> > Somewhat confused... isn't the LPEG module included using:
>> > require("lpeg")
>> > and not:
>> > require("re")
>> >
>> > Is it the regex emulation done using LPEG ?
>>
>> Yep, it's a separate module included with LPeg[1]. It's mostly PEG
>> notation[2] with a few Lua specific extras. I'm not sure if it was the
>> best example to use but I find it much clearer to read.
>>
>> [1]: http://www.inf.puc-rio.br/~roberto/lpeg/re.html
>> [2]: http://en.wikipedia.org/wiki/Parsing_expression_grammar#Examples
>>
>
The "re" module is mainly just an alternative syntax for LPeg.
You can use the main "lpeg" module to construct pattern objects with
the P, S and R functions and then use the overloaded operators,
captures and grammars to build up the complete pattern which can then
be matched against some input.
Alternatively, you can use the "re" module and let LPeg convert your
regex-style string into the equivalent patterns, captures, grammars,
etc. and match the returned pattern against some input.
I haven't used LPeg that much myself, so I can't provide any of my own
examples; but the documentation provides a few examples implemented
using both regular LPeg and the "re" module:
http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html#ex
http://www.inf.puc-rio.br/~roberto/lpeg/re.html#ex