Thomas A. Schmitz wrote:
You could also use a nice scripting language like ... lua!! lua has support for "balanced strings," so no nesting trickery is needed, see http://www.lua.org/pil/20.2.html (scroll to the bottom of the page). Or you could try and nag Hans into giving a nice tutorial on the lua lpeg library at Bohinj! :-)
Even in bare lua, it is very easy to match balanced braces: local data = "\\footnote{x{y}z}" local match = "\\footnote%s%b{}" local i = 0 while true do i,j = string.find(data, match, i+1) if i == nil then break end print(string.sub(data, i, j)) end with lpeg, the code looks a bit harder, but is still short and relatively shortforward: local matchtable = { "TEXT", SP = lpeg.S" \n\t"^0, BODY = lpeg.P{ "{" * ((1 - lpeg.S"{}") + lpeg.V(1))^0 * "}" }, FOOTNOTE = lpeg.P"\\footnote" * lpeg.V"SP" * lpeg.V"BODY" / print, TEXT = (lpeg.V"FOOTNOTE" + 1)^0 * -1, } lpeg.match(matchtable, data) Best wishes, Taco