lua-users home
lua-l archive

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




On Tue, Nov 11, 2014 at 9:58 PM, <meino.cramer@gmx.de> wrote:
Hi,

(I am no native english speaker...)

I looked (and tried to understand ...) Robertos video about the
concepts of lpeg/peg on Youtube and I read the lpeg tutorial
lpeg pages.

But still I am running against an inner wall...years of using regexps
could not be nullified that fast... ;)

One sentence of Robertos video stick in my head: Lpeg do not search.

How can I recognize the appearance of a certain pattern in a string
then?

For example:
mystinrg="CHaskellLispFortranBPCLAssemblerForthLuaSchemePerlCHILLTeXJavaJavascript"

Is it possible to check with LPeg and without missusing it , wheter "Lua"
is in that string and where (I know, that there are other lpegless methods to
do that ... thats why this is an example ;) ?

If this not possible or only can be "tricked"...how can LPeg react on
only partly known input formats?

Or is that a typical question of someone, who is still under the bad
influence of regexps?


Best regards,
Meino







Yes you can search. With lpeg, there are at least two methods that I can think of. The one that doesn't use grammars can be described thusly:

zero or more characters that do not equal "Lua" followed by "Lua"

or

(untested)

local P = lpeg.P

local contains_lua_pat = (P(1) - "Lua")^01 * P("Lua")

print(contains_la_pat:match(your_string))
--> position of the match 

If it succeeds, it returns the position of... i believe the first (and maybe also last) position of the match. If you use "lpeg.C", you'll get the capture.

--Andrew