Re: Finding matched parentheses

2013-07-31 Thread Geoff Canyon
On Tue, Jul 30, 2013 at 8:52 AM, Peter M. Brigham wrote: > function firstOffsetPair a,b,str >-- from dfepstein >-- str cannot be more than one line >-- returns first instance of char a && "matching" instance of char b in > str, or 0 if no a or empty if no match >put offset(a,str)

Re: Finding matched parentheses

2013-07-30 Thread Peter Haworth
On Mon, Jul 29, 2013 at 10:10 PM, Mark Wieder wrote: > "Pierre de Fermat also figured out how to do it, but the margin he was > writing in wasn't big enough for the code." > I think that's my favorite! I promise I will never, ever try to parse html with regex! Or if I do, I won't mention it her

Re: Finding matched parentheses

2013-07-30 Thread Peter M. Brigham
t; that kind of first "pair" and also > include nested pairs. That first pair is always minimally small. > > > The function could be easily modified to list all pairs within any designated > pair, of course. > > > > Anyway, it was fun to play with. > &

Re: Finding matched parentheses

2013-07-30 Thread Thierry Douez
2013/7/29 Geoff Canyon > On Mon, Jul 29, 2013 at 12:14 PM, Peter Haworth wrote: > > > Personally, I'd use the regex that Thierry posted a couple of days back. > > No recursion involved and one line of code does the job. > > > > > If you're talking about: > > matchChunk(mystring,"(\([^)]*\)).*",

Re: Finding matched parentheses

2013-07-29 Thread Peter Haworth
On Mon, Jul 29, 2013 at 6:57 PM, wrote: > I may not have correctly implemented Peter Haworth's Regex string, but I > get wrong answers from writing the function as below -- > offsetPair6("(a(a))") returns "2 5". Hi David, The regex actually returns the string within the parens so to get the cha

Re: Finding matched parentheses

2013-07-29 Thread Mark Wieder
Geoff Canyon writes: > > regex is notoriously unable to handle recursion. To see endless heated > debate, search the web for how to parse HTML using regex. I found an old posting of my own on this topic... "The cannot hold it is too late."

Re: Finding matched parentheses

2013-07-29 Thread dfepstein
Testing for speed, I found that my July 29 function--the one that uses lineDelimiter and itemDelimiter--is thwarted when two parens appear as consecutive characters, and gives the wrong answer in that case. Sorry about that. I may not have correctly implemented Peter Haworth's Regex string,

Re: Finding matched parentheses

2013-07-29 Thread Peter Haworth
On Mon, Jul 29, 2013 at 1:15 PM, 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

Re: Finding matched parentheses

2013-07-29 Thread dunbarx
st pair is always minimally small. The function could be easily modified to list all pairs within any designated pair, of course. Anyway, it was fun to play with. Craig -Original Message- From: Peter Haworth To: How to use LiveCode Sent: Mon, Jul 29, 2013 1:15 pm Subject: Re: Finding ma

Re: Finding matched parentheses

2013-07-29 Thread Geoff Canyon
On Mon, Jul 29, 2013 at 12:14 PM, Peter Haworth wrote: > Personally, I'd use the regex that Thierry posted a couple of days back. > No recursion involved and one line of code does the job. > If you're talking about: matchChunk(mystring,"(\([^)]*\)).*",tstart,tEnd) That will return 1,4 for th

Re: Finding matched parentheses

2013-07-29 Thread Peter Haworth
On Mon, Jul 29, 2013 at 9:07 AM, wrote: > I tired your script on the string: > > > > aa(ss)(xx)(yy) > > > > it only returned the parens bracketing "ss" > I Think that's what he wants to do - just find the position of the first set of parentheses, taking nested parens into account. But not sure.

Re: Finding matched parentheses

2013-07-29 Thread dunbarx
Hi. I tired your script on the string: aa(ss)(xx)(yy) it only returned the parens bracketing "ss" Craig Newman -Original Message- From: dfepstein To: use-livecode Sent: Mon, Jul 29, 2013 10:56 am Subject: Re: Finding matched parentheses On second thought :

Re: Finding matched parentheses

2013-07-29 Thread dfepstein
On second thought : function offsetPair a,b,str -- str cannot be more than one line    -- returns first instance of char a && "matching" instance of char b in str, or 0 if no a or empty if no match    put offset(a,str) into ca    if ca = 0 then return 0    put numToChar(7) into char 1 to

Re: Finding matched parentheses

2013-07-28 Thread Peter Haworth
On Sun, Jul 28, 2013 at 1:28 PM, Thierry Douez wrote: > Having using them for years, I still think they are efficient :) > > > But as any tools, you have to use them for the right purpose.. > > I agree with you on that. > > Some of the tests I did resulted in LC running out of memory > > too! T

Re: Finding matched parentheses

2013-07-28 Thread David Epstein
Thanks for all the interesting replies to this query. I used "offset" rather than (what most replies suggested) "repeat" with (or for) each character thinking it would be faster, but that seems to be true only if the string is extremely long and the sought for character very far from the f

Re: Finding matched parentheses

2013-07-28 Thread Thierry Douez
H ​i,​ 2013/7/28 Peter Haworth > Yes, I can believe that. don't think regex is the most efficient thing in > the world. ​Having using them for years, I still think they are efficient :)​ ​But as any tools, you have to use them for the right purpose.. ​ Some of the tests I did resulted in L

Re: Finding matched parentheses

2013-07-28 Thread dunbarx
I will gladly accept being clever and inefficient. Craig -Original Message- From: Peter Haworth To: How to use LiveCode Sent: Sun, Jul 28, 2013 2:00 pm Subject: Re: Finding matched parentheses Yes, I can believe that. don't think regex is the most efficient thing in the

Re: Finding matched parentheses

2013-07-28 Thread Peter Haworth
On Sun, Jul 28, 2013 at 12:56 AM, Thierry Douez wrote: > I > dropped the idea of using (?R) or its derivatives because for most > practical cases > it didn't worked as expected (even crash LC) but > those regex > worked in Perl scripts! > (looks like in php it works great too). > Yes, I saw som

Re: Finding matched parentheses

2013-07-28 Thread Peter Haworth
t; > end repeat > >end repeat > >sort accum numeric > >return accum > > end findNests > > > > > > The returned list orders the char numbers of the paired parens. > > > > > > Craig > > > > > > > >

Re: Finding matched parentheses

2013-07-28 Thread Thierry Douez
2013/7/27 Peter Haworth > On Fri, Jul 26, 2013 at 10:10 AM, Thierry Douez > wrote: > > > There are solutions for recursive pattern matching with PCRE, but > > never being able to make them work within LC. > > > > Hi Thierry, > If you follow the link in my post to > Geoff, there's an example of a

Re: Finding matched parentheses

2013-07-28 Thread Geoff Canyon
numeric >return accum > end findNests > > > The returned list orders the char numbers of the paired parens. > > > Craig > > > > -Original Message- > From: Peter Haworth > To: How to use LiveCode > Sent: Fri, Jul 26, 2013 12:09 pm > Subject: Re

Re: Finding matched parentheses

2013-07-27 Thread Peter Haworth
On Fri, Jul 26, 2013 at 10:10 AM, Thierry Douez wrote: > There are solutions for recursive pattern matching with PCRE, but > never being able to make them work within LC. > Hi Thierry, Sorry, missed your post somehow. If you follow the link in my post to Geoff, there's an example of a recursive

Re: Finding matched parentheses

2013-07-27 Thread Peter Haworth
On Thu, Jul 25, 2013 at 9:53 PM, Geoff Canyon wrote: > regex is notoriously unable to handle recursion. To see endless heated > debate, search the web for how to parse HTML using regex. > Hi Geoff, You piqued my interest and indeed there are endless debates about parsing html! I also came acros

Re: Finding matched parentheses

2013-07-26 Thread Thierry Douez
2013/7/26 Peter Haworth > On Fri, Jul 26, 2013 at 12:10 AM, Thierry Douez > wrote: > > > But this one will work and match the first occurence: > > > > if matchChunk(mystring,"(\([^)]*\)).*",tstart,tEnd) > > > > Regards, > > > > Thierry > > > > Nice Thierry! Doesn't handle nested parens though

Re: Finding matched parentheses

2013-07-26 Thread dunbarx
end repeat end repeat sort accum numeric return accum end findNests The returned list orders the char numbers of the paired parens. Craig -Original Message- From: Peter Haworth To: How to use LiveCode Sent: Fri, Jul 26, 2013 12:09 pm Subject: Re: Finding matched parentheses On Fr

Re: Finding matched parentheses

2013-07-26 Thread Peter Haworth
On Fri, Jul 26, 2013 at 12:10 AM, Thierry Douez wrote: > But this one will work and match the first occurence: > > if matchChunk(mystring,"(\([^)]*\)).*",tstart,tEnd) > > Regards, > > Thierry > Nice Thierry! Doesn't handle nested parens though, but once again, that may not be a possibility in

Re: Finding matched parentheses

2013-07-26 Thread Peter Haworth
On Thu, Jul 25, 2013 at 7:38 PM, Mark Wieder wrote: > 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" > OK, I thought

Re: Finding matched parentheses

2013-07-26 Thread Geoff Canyon
You have to account for the nesting of parens, something like: ((())) Sent from my iPad On Jul 26, 2013, at 8:17 AM, "Peter M. Brigham" wrote: > If you want a more general solution, you could use offsets(a,str) along with > offsets(b,str) and look at all the pairs, with item i of offsets(b,str

Re: Finding matched parentheses

2013-07-26 Thread Peter M. Brigham
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 offse

Re: Finding matched parentheses

2013-07-26 Thread Peter M. Brigham
Not sure what you are looking for here. Just find first occurrence of a in str and return the offset of the next occurrence of b after that? function offsetPair a,b,str,@z -- returns offset(a,str) and loads in z offset(b,str) where b is the "matching" member of the pair; -- returns 0 if th

Re: Finding matched parentheses

2013-07-26 Thread Geoff Canyon
Arrgh. Well it certainly pays to check your output... I wasn't clearing one of the variables in the code I wrote outside the function to test it, hence the false good results in the first email. Here's the output once that was fixed. Hopefully I'm not overlooking something else. put stripHTML(

Re: Finding matched parentheses

2013-07-26 Thread Geoff Canyon
The first answer to http://stackoverflow.com/questions/590747/using-regular-expressions-to-parse-html-why-notexplains the disconnect between regex and HTML better than I can. Here is a function that still requires correctness -- any unmatched ( or ) will cause it to return E with the position of t

Re: Finding matched parentheses

2013-07-26 Thread Thierry Douez
2013/7/26 Mark Wieder > 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" > ​But this one will work and match the first occurence:​ if matchChunk(mystring,"(\([^)]*\)).

Re: Finding matched parentheses

2013-07-25 Thread Mark Wieder
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. > He

Re: Finding matched parentheses

2013-07-25 Thread stephen barncard
look like sneezing On Thu, Jul 25, 2013 at 10:14 PM, Jerry Jensen wrote: > Gesundheit! > > On Jul 25, 2013, at 9:53 PM, Geoff Canyon wrote: > > > > erksdfkj(klwer(jklsdfljk)lkjsdflj)lsdjkfklsd > > 9,34 > > > > ljksaljk()ljkadsflj > > 9,18 > > > > (()(()((()(()()((())()((((

Re: Finding matched parentheses

2013-07-25 Thread Jerry Jensen
Gesundheit! On Jul 25, 2013, at 9:53 PM, Geoff Canyon wrote: > > erksdfkj(klwer(jklsdfljk)lkjsdflj)lsdjkfklsd > 9,34 > > ljksaljk()ljkadsflj > 9,18 > > (()(()((()(()()((())()(((()((( > 1,32 > 33,42 > > aslkjsadflj(asldkjf(jklsdf)(sjlkdf)(ljksdf))lskjdf > 12,44 > > asdlfkj

Re: Finding matched parentheses

2013-07-25 Thread Geoff Canyon
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 appropr

Re: Finding matched parentheses

2013-07-25 Thread Mark Wieder
Pete- Thursday, July 25, 2013, 6:52:15 PM, you wrote: > This appears to work: > get matchChunk(,".*\(.*(\)).*",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

Re: Finding matched parentheses

2013-07-25 Thread Peter Haworth
This appears to work: get matchChunk(,".*\(.*(\)).*",tstart,tEnd) tStart and tEnd will both contain the offset of the closing paren You'd have to build the regex on the fly if you want to match any pair of characters but the regex for square brackets is ".*\[.*(\]).*" Pete lcSQL Software

Re: Finding matched parentheses

2013-07-25 Thread Mark Wieder
David- Thursday, July 25, 2013, 5:17:20 PM, you wrote: > Has anyone scripted a function that will locate the closing > parenthesis (or bracket, etc.) of a pair? Below is my effort. > Reports of its limitations or simpler alternatives (regex?) are invited. Here's the one I wrote for glx2. I

Finding matched parentheses

2013-07-25 Thread David Epstein
Has anyone scripted a function that will locate the closing parenthesis (or bracket, etc.) of a pair? Below is my effort. Reports of its limitations or simpler alternatives (regex?) are invited. David Epstein function offsetPair a,b,str,@z -- returns offset(a,str) and loads in z offset(b