Finding matched parentheses
Peter M. Brigham
pmbrig at gmail.com
Fri Jul 26 09:33:06 EDT 2013
Sorry, in my previous post I did not consider nested parens (I was thinking English text, not C code). The function below should return the offset of the outermost closing paren, the "match" for the first opening paren.
function offsetPair a,b,str
-- returns offset(a,str) and loads in z offset(b,str) where b is the "matching" member of the pair;
-- returns 0 if there is no a, empty if there is no match
-- offsets(b,str) returns a comma-delimited list of all the offsets
-- of a in str (see below)
put offsets(a,str) into startOffsets
if startOffset = 0 then return 0
put offsets(b,str) into endOffsets
if endOffsets = 0 then return empty
if the number of items of startOffsets <> the number of items of endOffsets then
-- unmatched parens, not grammatical
return empty
end if
put item -1 of endOffsets into lastCloseParen
if lastCloseParen < item 1 of startOffsets then return empty
-- right number of open + close parens
-- but not grammatical
return item -1 of endOffsets
end offsetPair
-- Peter
Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig
On Jul 26, 2013, at 1:35 AM, Mark Wieder wrote:
> Geoff-
>
> Thursday, July 25, 2013, 9:53:47 PM, you wrote:
>
>> regex is notoriously unable to handle recursion. To see endless heated
>> debate, search the web for how to parse HTML using regex.
>
> To be fair, the blame is mostly on HTML, not regex. But there's enough
> blame to go around. No worries.
>
>> Here is a fairly short function that searches for the outermost matched
>> pairs of characters.
>
> "outermost" is different from matched. You might want to find the
> matching parenthesis for "lineDelim()" in
>
> put stripHTML(
> url("http:://www.www.com"),
> lineDelim("/n"
> ),
> cos(theta)
> )
>
> --
> -Mark Wieder
> mwieder at ahsoftware.net
>
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
More information about the use-livecode
mailing list