regular expressions

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
gdp
Posts: 2
Joined: Wed Apr 01, 2009 3:35 pm
Contact:

regular expressions

Post by gdp » Wed Apr 01, 2009 4:02 pm

I'm in trouble using function "matchChunk()" - it seems not working...

In a fld "tradTXT" there is that line:

\marginal{Térence \cite{terHea},\emph{Heautontim.}, III, 5.}\\

I first declare two local variables: startChar, endChar and put empty in them in a btn script.

I write that code in the same btn script:
on mouseup
put matchChunk(field "tradTXT","\\marginal{.*",startChar,endChar)
put startChar&&endChar into fld "result"
end mouseup

And... I never get any value. When debbuging, variables are always empty
Where am I wrong?

had anyone encountered difficulties with "matchChunk"?
Thanks...
GdP

mroam
Posts: 11
Joined: Tue Nov 27, 2007 8:11 pm
Contact:

matchChunk

Post by mroam » Wed Apr 01, 2009 7:40 pm

Hi,
your regular expression needs parentheses. (I had to read the documentation 3 or 4 times before I realized that's what was missing.)

I built a simpler example:

Code: Select all

on mouseUp
   local startM, endM
   put 0 into startM
   put 0 into endM
   #following returns true and startM and endM are 3 and 4
   answer matchChunk("wanda","(nd)",startM,endM) & "," & startM & "," & endM
end mouseUp
which seems to work.
Your example works with a slight change -- parentheses around the regular expression--

Code: Select all

put matchChunk(field "tradTXT","(\\marginal{.*)",startChar,endChar)
Hope this helps,
--Mike

gdp
Posts: 2
Joined: Wed Apr 01, 2009 3:35 pm
Contact:

Post by gdp » Wed Apr 01, 2009 7:48 pm

Many thanks!...
Yes, now it works!
I don't thought that parentheses were necessary in that case...

"OUF" (in french.. ;-)
GdP

Bernard
Posts: 351
Joined: Sat Apr 08, 2006 10:14 pm

Post by Bernard » Fri Apr 03, 2009 10:03 am

Hi,

I think Revolution's built-in documentation is a bit weak (to say the least) when it comes to regular expressions. And whilst the dictionary entry for matchChunk does say "If the regularExpression includes a pair of parentheses, the position of the substring matching the part of the regular expression inside the parentheses is placed in the variables in the positionVarsList", but unfortunately the example immediately above that does not show any parenthesis in the pattern (it shows a variable holding the pattern).

Revolution's regex support is built using PCRE. Unfortunately, the gap between what's provided by Revolution as regex documentation, and what's provided by http://www.pcre.org/pcre.txt is huge. Ideally one wouldn't need to plough through pcre.txt for anything but complex regex.

I've generally found that I need to preface the regex pattern in Revolution with "(?im)", so that the match is case insensitive (as Rev is by default), and so that I can match on the beginnings and endings of lines. That way, I don't find it such a mismatch between using Rev's way of handling 'text chunks' with the regex way.

Post Reply