lua-users home
lua-l archive

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

On 20/05/2012 16:11, Roberto Ierusalimschy wrote:
But the following doesn't, the function gets all chars, not only the
capital ones (those supposed to be captured):

local patt = (findCap / function (a) return string.lower(a) end)^0
print(m.match(patt, toMatch))

Why the behavior of the pattern is changed by the associated function?

In all cases, the function is called once everytime there is a match.

Aah, that's something that totally escaped me when reading the description of the function capture.

In the second case, there is match for each character (because of the
(1 - cap) in 'findCap').

That's subtle!

When the pattern of a function capture
produces no captures, the entire match is given as the captured value.
Maybe this is what you whant:

local findCap = (1 - cap) + m.C(cap) / string.lower

Now the function is called only when 'm.C(cap)' matches.

Ah, yes, the final pattern would then be:
local patt = ((1 - cap) + m.C(cap) / string.lower)^0

I understand the nuance now, thanks!

Philippe Lhoste
--  (near) Paris -- France
--  --  --  --  --  --  --  --  --  --  --  --  --  --