Finding matched parentheses
Peter Haworth
pete at lcsql.com
Mon Jul 29 19:21:36 EDT 2013
On Mon, Jul 29, 2013 at 1:15 PM, <dunbarx at aol.com> wrote:
> I read the original post as finding the "closing parenthesis ... of a pair"
You're right, sorry, forgot which regex was the one that worked. It was
the following (which I found online so no credit to me):
\((((?>[^()]+)|(?R))*)\)
This uses regex look ahead and recursion to solve the problem. It only
works for the first set of nested parens in a line but since the OP's
function doesn't handle multiple sets, I assume that's not a requirement.
If it is, then here's a function that seems to work with limited testing
and subject to the performance limitations that Geoff mentioned. Apologies
in advance for any extraneous empty line and/or asterisks that gMail might
insert into the function!
*function* getCharPairPositions pstring,pstartchar,pendchar
*local* tstart,tend,tResults,tAdj,tregex
*put* zero into tAdj
*put* "\" & pstartchar & "(((?>[^" & pstartchar & pendchar &
"]+)|(?R))*)\" & pendchar into tRegex
*repeat*
*if* matchChunk(pstring,tRegex,tStart,tEnd) *then*
*put* tStart-1+tAdj, tEnd+1+tAdj & *return* after tResults
*add* (tEnd+1) to tAdj
*delete* char 1 to tEnd+1 of pstring
*else*
*exit* *repeat*
*end* *if*
*end* *repeat*
*return* tResults
*end* getCharPairPositions
Pete
lcSQL Software <http://www.lcsql.com>
More information about the use-livecode
mailing list