\usemodule[pre-30]

\StartSample{Matching}

\startbuffer
\lua {
    match = {} ;
    match.expression = "" ;
    match.string = "" ;
    match.result = {} ;
    match.start = 0 ;
    match.length = 0 ;
}

\lua {
    function match.find(expr, str)
        match.expression = expr ;
        match.string = str ;
        match.result = {} ;
        match.start, match.length,
            match.result[1], match.result[2], match.result[3],
            match.result[4], match.result[5], match.result[6],
            match.result[7], match.result[8], match.result[9] =
                string.find(match.string,match.expression) ;
    end
}

\def\matchstring#1#2{\lua{ match.find("#1","#2") }}

\def\matchresult  #1{\lua{ tex.print(match.result[#1]) }}
\stopbuffer

\setups{show-definition}

\startbuffer
\matchstring
  {(\letterpercent d+) (\letterpercent d+) (\letterpercent d+)}
  {2005 08 08}

\starttabulate[|l|c|r|]
\NC year  \NC \matchresult{1} \NC \number \matchresult{1} \NC \NR
\NC month \NC \matchresult{2} \NC \number \matchresult{2} \NC \NR
\NC day   \NC \matchresult{3} \NC \number \matchresult{3} \NC \NR
\stoptabulate
\stopbuffer

\setups{show-usage}

\startbuffer
\matchstring
  {0*(\letterpercent d+) 0*(\letterpercent d+) 0*(\letterpercent d+)}
  {2005 08 08}

\starttabulate[|l|c|]
\NC year  \NC \matchresult{1} \NC \NR
\NC month \NC \matchresult{2} \NC \NR
\NC day   \NC \matchresult{3} \NC \NR
\stoptabulate
\stopbuffer

\setups{show-usage}

\StopSample
