Pattern matching

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Pattern matching

Gavin Wraith-2
This is an appeal for help with pattern matching. I appreciate
that one can ask too much of the pattern matching provided
in stringlib, and that for things like syntax colouring one
really needs a full-blooded parser. However, maybe there are
some wizards out there who can save me a lot of mental effort.
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?
It would be useful to have a little dictionary of useful
patterns on the Lua website.
-- 
Gavin Wraith ([hidden email]) or ([hidden email])
Home page: http://www.wraith.u-net.com/


Reply | Threaded
Open this post in threaded view
|

Re: Pattern matching

Pedro Miller Rabinovitch-3
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

Reply | Threaded
Open this post in threaded view
|

Re: Pattern matching

Reuben Thomas-4
In reply to this post by Gavin Wraith-2
> This is an appeal for help with pattern matching. I appreciate
> that one can ask too much of the pattern matching provided
> in stringlib, and that for things like syntax colouring one
> really needs a full-blooded parser. However, maybe there are
> some wizards out there who can save me a lot of mental effort.
> 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?

To do this neatly you need alternation, so use my rexlib (URL below) rather
than Lua's built-in patterns. (I've ported the underlying library to RISC OS
and EPOC.)

-- 
http://sc3d.org/rrt/ | Slow Pedestrian Crossing


Reply | Threaded
Open this post in threaded view
|

Re: Pattern matching

Reuben Thomas-4
In reply to this post by Gavin Wraith-2
> How do I specify a pattern for a Lua keyword?

I should say that I read this as "a *valid* Lua keyword" rather than
something of the correct lexical form, i.e. the pattern "for|do|function|..."

-- 
http://sc3d.org/rrt/ | Careful Cyclists Approaching From Right


Reply | Threaded
Open this post in threaded view
|

Re: Pattern matching

Gavin Wraith-2
In reply to this post by Pedro Miller Rabinovitch-3
On Wed 14 Mar, Pedro Miller Rabinovitch wrote:
 
> 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!)

Thanks - what I really wanted was a pattern to match, say, the
occurrences of the word 'end' in a Lua script (and for each of
the other reserved words). 
-- 
Gavin Wraith ([hidden email]) or ([hidden email])
Home page: http://www.wraith.u-net.com/