lua-users home
lua-l archive

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


On Mon, Nov 25, 2013 at 1:28 PM, Georgios Petsagourakis
<petsagouris+lual@gmail.com> wrote:
> but it produces nothing. Can anyone give me a hand?


local extension = lpeg.P('.') * lpeg.P(1 - lpeg.P('.')) * lpeg.P(-1)

Here, you don't say that you can have more than one character after
the dot. Instead

local extension = (P('.') * (P(1) - P('.'))^1) * -1
(i remove the `lpeg.` for brevity)

we say "a dot, followed by any character not a dot, one or more times,
followed by the end"

local filename_characters = lpeg.P(lpeg.P(1) -lpeg.S('\\?|"<>'))^1

here you're probably okay, except that I tend to define singletons
first and then how they repeat. In the extension example, one
extension is one or more chars. Here, we just want to know what a
file_name_char is and then we can say that we want more than one of
them later, when we define a filename:

local filename_character = lpeg.P(lpeg.P(1) -lpeg.S('\\?|"<>'))


Next, you've got:

local filename = lpeg.P(1 - lpeg.P(extension))

I've changed my example to follow having just one character. This
turns out to be essential, because I need a filename_character that is
not an extension, one or more times, followed by an extension.
Remember, the extension is defined as being "at the end". As you know
(by looking at your code), this is important.

My version includes the capture of both the file name and the extension:


local filename = C(P((filename_character - extension)^1)) * C(extension)

Here is the working code:

```lua

local lpeg = require'lpeg'

-- / \ ? % * : | " < > .


local t1 = "file.name.ext" --> file.name  ext
local t2 = "file.ext"      --> file       ext

--At the moment I am trying the following:
local P = lpeg.P
local C = lpeg.C
local S = lpeg.S

local extension = (P('.') * (P(1) - P('.'))^1) * -1

print(extension:match(".ext"))

local filename_character = P(P(1) - S('\\?|"<>'))
print(filename_characters:match(t1))

local filename = C(P((filename_character - extension)^1)) * C(extension)

print(filename:match(t1))

``

Let me know if I can make anything clearer! I live to help with lpeg! :)

-Andrew