[luatex] Library for regular expressions in lualatex
Henri Menke
henrimenke at gmail.com
Tue Jun 11 00:33:23 CEST 2019
On 10/06/2019, 18:42:06, Саша Козловский wrote:
> Hello Everybody. I want in each formula replace all $$ on \[\] and $ on \(\)
> if before each $ it no char \,because it not exists any ways to override $
> and $$ in lualatex. For example instead of formula $$a+b=c$$ must be formula
> \[a+b=c\] and,for example,instead of formula $a-b=0$ must be formula
> \(a-b=0\). All replacement i do in callback process_input_buffer. So i need
> in regexp,but lua in lualatex is not fully support regular expressions,so
> advice me please library,which use sintaksis of regular expressions like in
> python and which works in lualatex. Thanks everybody for the help.
LuaTeX comes with built-in support for the LPEG parsing library. The
LPEG library implements parsing expression grammars (PEG) as an embedded
domain specific language (EDSL) using operator overloading and
metatables in Lua. At TUG2019 I will give a talk on how to use it.
https://tug.org/tug2019/abstracts/menke-lpeg.txt
Anyway, back to your question about regular expressions. There is the
re module built on top of LPEG that provides regular expressions in Lua.
It is written in pure Lua and you can download it from the LPEG website
http://www.inf.puc-rio.br/~roberto/lpeg/re.html
Note that the re module neither implements Perl-compatible nor POSIX
regular expressions, but its own flavour. The website also has usage
examples.
However, regular expressions won't solve your problem. You want to
replace *matching* $...$ by \(...\). But if you input is
$a + b * \text{d $e$ f} + g$
regular expressions will turn it into
\(a + b * \text{d \)e\( f} + g\)
which is obviously wrong. Regular expressions cannot match balanced
delimiters because that is inherently irregular. You might want to
reconsider your approach. You could maybe write an LPEG grammar to do
what you want but because the opening and closing delimiter are the same
I think you will have problems with left-recursion.
Cheers, Henri
More information about the luatex
mailing list