lua-users home
lua-l archive

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


How do I specify a pattern for a Lua keyword?
How do I say that either the keyword begins a line or is
preceded by a character matching [^%w_], and that either
it ends a line or is followed by a character matching [^%w_],
for example?

Let's hope I didn't get this wrong. From what I read above, in:

	txt = "This is a test.Another\n_word1.2works.well"

You want to match:

This
is
a
test
Another
_word1
well

Hence not matching `2works'. Right?

See if this works for you:

	ptrn = "[^%w_]([%a_][%w_]*)"
	gsub("\000"..txt, ptrn, print)

The "\000" at the beginning anchors the first answer correctly, since (AFAIK) we can't make use of '^'. Note that I haven't added the ending "[^%w_]" at the end of the pattern, since it would mean trouble with words separated by a single character. But it should work fine, anyway, since gsub() tries to match as many characters as possible.

It would be useful to have a little dictionary of useful
patterns on the Lua website.

Agreed! People should send them in to lhf for the next LTN. :-) I'll see if I have any interesting ones sitting around here.

Hope I've been of assistance,
(and not totally off-track!)

	Pedro.

--
Pedro Miller Rabinovitch
Gerente Geral de Tecnologia
Cipher Technology
www.cipher.com.br