looking for a Lua-based solution for splitting a string into two or more components

Viewed 222

This is my first posting to this site, so please bear with me.

Consider the following, somewhat representative string:

fld u.a. ldfjal \verb*u.a.* dlf \lstinline$u.a.$ u.a. dfla \url{u.a.}rrr

For some background: \verb*....* and \lstline$...$ are LaTeX macros whose arguments aren't delimited by matching curly braces but, instead, by a common character: * in the case of \verb, and $ in the case of \lstinline. An important point is that the delimiter characters can be any printable ASCII character except { and }; one should not assume that * or $ will be used as delimiters in all (or even any) cases. Separately, \url{...} is a LaTeX macro whose argument is delimited by curly braces. The full string should be assume to contain utf8-encoded characters; for simplicity, let's assume they're pure ASCII characters.

I'm looking to create a (hopefully reasonably efficient...) Lua-based way to split the full string into two sets of substrings: (a) the parts that consist of LaTeX macros and their associated arguments and (b) the other parts. The eventual goal is to feed the "other parts" to a string.gsub function call.

Turning to the preceding example, how might one separate the string

fld u.a. ldfjal \verb*u.a.* dlf \lstinline$u.a.$ u.a. dfla \url{u.a.}rrr

into "Y" (inside a verbatim-like macro) and "N" (not inside a verbatim-like macro) components, i.e.,

NNNNNNNNNNNNNNNNYYYYYYYYYYYNNNNNYYYYYYYYYYYYYYYYNNNNNNNNNNNYYYYYYYYYYNNN

Oh, each full string is guaranteed to have "N" components, but there may be no "Y" components. The string may, in principle, start and end with either "N" or "Y" components.

I've been trying to come up with a solution that uses Lua's string library functions, but haven't gotten far at all. :-(

3 Answers
Related