Page 1 of 1

regular expressions

Posted: Wed Apr 01, 2009 4:02 pm
by gdp
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...

matchChunk

Posted: Wed Apr 01, 2009 7:40 pm
by mroam
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

Posted: Wed Apr 01, 2009 7:48 pm
by gdp
Many thanks!...
Yes, now it works!
I don't thought that parentheses were necessary in that case...

"OUF" (in french.. ;-)

Posted: Fri Apr 03, 2009 10:03 am
by Bernard
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.