Le 03/12/2010 12:08, Philipp Gesang a écrit :
On 2010-12-03<10:37:02>, Paul Isambert wrote:
Le 03/12/2010 00:22, Philipp Gesang a écrit :
If you encounter problems with magic characters in patterns,
there is some assistance waiting in the context helper libs:
http://wiki.contextgarden.net/String_Manipulation#string.escapedpattern.28string.29_.7C_string.partialescapedpattern.28string.29
Are you trying to lure me into ConTeXt? :)
Nice to get inspiration from, though.
Well, not *actively* so -- but it would appear that context of
itself has some involuntarily alluring property that does the job
pretty well.
ConTeXt has tons of ``involuntarily'' alluring properties anyway,
besides string manipulation. As far as I'm concerned, though, it has one
big drawback: I didn't write it. So when I want it to stop doing this or
that, I can't just fix some lines of code. Ok, now you might tell me:
you'll never want ConTeXt to stop doing this or that, because it is so
good -- but you don't know the level of perversion I sometimes reach :)
PS: What’s the problem with lpeg, anyways?
Nothing, but I don't know how to use it and it seems to me it can't
be used lightly; and for the moment I have other things to do.
Your code, for instance, which seems to involve a so-called grammar,
looks really interesting, except I don't know what lpeg.Cmt,
lpeg.Cs, etc., mean, so to me it's unreadable. [Ok, from what I can
read, it does the same thing as the above function.]
“Cs” triggers “smooth” substitution inside its scope. Without it
you would get a lose bunch of substitutes missing the rest of
the string in between.
“Cmt” gives you free hand to create your very own personalized
matching function -- it does match, if the pattern it
encounters is matched by the first argument, but also checks
if the second arg -- the function -- returns true. This makes
it the most versatile of any of lpeg’s functions (not
omnipotent, though …) as you can feed it arbitrary conditions
(imagine patterns that match only on Mondays, call a routine
to check your bank account before matching, or match iff
there is an entry in an online dictionary for the match as
filtered through an md5 function …).
(Details at Roberto’s: http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html)
Wrapping things up in a grammar saves rules which can become
necessary in bigger patterns (have a look at
http://lua-users.org/lists/lua-l/2008-11/msg00470.html) but in
this case you could do without it.
Switching to lpeg means freeing yourself from the limitations
(and idiosyncratic notation) of the common string patterns. It
surely *can*, as you put it, be used lightly once you got the
hang of it.
The problem is precisely getting the hang. Actually I'll switch to lpeg
one day or another. I understand not using it is like sticking to
\lastbox to analyze a paragraph, but for the moment I want something
done that'll make do with the basic string functions. But thank you, the
code you gave will be a good place to start to understand it (I don't
know why, I have a very low tolerance threshold to Roberto's lpeg page;
I find it extremely interesting for 15 minutes, then get upset...).
Best,
Paul