Finding matched parentheses

Geoff Canyon gcanyon at gmail.com
Fri Jul 26 00:53:47 EDT 2013


regex is notoriously unable to handle recursion. To see endless heated
debate, search the web for how to parse HTML using regex.

Here is a fairly short function that searches for the outermost matched
pairs of characters. For parentheses, that means that every ( must be
balanced by a ), in appropriate order. It is happy with multiple matches,
and returns them all, so the returned value can be a set of lines of
start,stop values

function strictOuterMatching S,B,E
   put 0 into C
   repeat with i = 1 to length(S)
      if char i of S is E then
         if C < 1 then return "E" && i
         if C = 1 then put i & cr after R
         subtract 1 from C
      else if char i of S is B then
         if C = 0 then put i & comma after R
         add 1 to C
      end if
   end repeat
   if C = 0 then return R else return "E" && i
end strictOuterMatching

samples from my testing:

erksdfkj(klwer(jklsdfljk)lkjsdflj)lsdjkfklsd
9,34

ljksaljk((((()))))ljkadsflj
9,18

(()(()((()(()()((())()(())))))))(()((())))
1,32
33,42

aslkjsadflj(asldkjf(jklsdf)(sjlkdf)(ljksdf))lskjdf
12,44

asdlfkj(adfjlk)sf(()())sdf(()())()sdflkj
8,15
18,23
27,32
33,34

ljkewr(((()))
E 13

23l4jk)))))
E 7

(jlksdfjkl)))
E 12

lakjsdfljasdfljkasdf

asdlfkjasdlkjf(sjkldf(sjkldf(ljksdfjl)lkjsdf(sldkjf)sldjf)slkdjf
E 64

asdflkj)ksdf)ljksdf(skldf)
E 8

hksakdjfsdf







On Thu, Jul 25, 2013 at 9:38 PM, Mark Wieder <mwieder at ahsoftware.net> wrote:

> Pete-
>
> Thursday, July 25, 2013, 6:52:15 PM, you wrote:
>
> > This appears to work:
>
> > get matchChunk(<string>,".*\(.*(\)).*",tstart,tEnd)
>
> I think this is closer to what you're looking for
>
> get matchChunk(pString,"(\(.*\)).*",tstart,tEnd)
>
> but even that will fail when you have multiple parentheses in strings
> like this:
>
> "hello (bucko) this (is) a test"
>
> --
> -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