[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: lpeg pattern repetition
- From: Nick Gammon <nick@...>
- Date: Mon, 2 Jun 2008 08:30:36 +1000
Hi,
I have been trying out lpeg, and am wondering if there is an easy way
of representing the notion of something like this:
I see 4567 cattle
... where the number is a 4 to 6-digit number.
In PCRE I would write it like this:
I see [0-9]{4,6} cattle
The best I could come up with in lpeg is:
require "lpeg"
digits = lpeg.R "09"
patt = lpeg.P "I see " * digits * digits * digits * digits *
digits^-2 * " cattle"
print (lpeg.match (patt, "I see 4567 cattle")) --> 18
This requires the explicit repetition of the digits four times,
followed by no more than 2 extra ones.
Is there an easier or less wordy way of doing this? I know I can use a
"for" loop to concatenate "digits", but apart from that?
- Nick Gammon