Page 1 of 1

Simple Parse and Replace

Posted: Wed Feb 28, 2018 11:31 pm
by andyt
I've been trying to create a simple utility that takes a document loaded with placeholders wrapped in "$%", for example $%name%$ and replace each placeholder with a value taken from the UI. I have however fallen at the first hurdle - parsing the document.

I have two fields and a button coded as follows:

on mouseUp
local tTemplateSource,tTemplateTags,tLine
put field "template_source" into tTemplateSource
filter tTemplateSource with regex pattern "$%+[a-z]+%$" into tTemplateTags
put tTemplateTags into field "template_tags"
end mouseUp

Absolutely nothing appears in the template_tags field. What is wrong with the filter statement above - I've tried all sorts of variations of it taken from the Livecode dictionary?

How would experienced coders approach this task?

Re: Simple Parse and Replace

Posted: Thu Mar 01, 2018 12:15 am
by dunbarx
Hi. If I understand you correctly, say you have some text in fld 1, like:
Now is the time $%adam$% for all good men $%Eve$% to come to the aid $%Snake$% of their country..
You might do it this way:

Code: Select all

on mouseup
   get fld 1
     set the itemDel to "$%"
   put"Tom$%$%Dick$%$%Harry" into tNewNames
 
   repeat with y = 1 to the number of items of it step 2
      put item y of tNewNames into item y + 1 of it
   end repeat
   answer it
end mouseup
This depends mightily on the exact structure you outlined.

There are others. It would be much easier if the trailing delimiter was different, say "%$". In that case you could do something like this (pseudo):

Code: Select all

 put offset("$%",fld 1) into startChar
 put offset("%$",fld 1) into endChar
 put newData into char startChar to endChar + 1 of fld 1
There are others...

Craig Newman

Re: Simple Parse and Replace

Posted: Thu Mar 01, 2018 2:55 am
by [-hh]
Why don't you use merge?
The delimiters are then [[...]], for example [[myVariable]] or [[line 2 of fld 1]].
Now merge(strng) does all replacements in strng by one call.
No need to parse strng for the delimiters by yourself.

Re: Simple Parse and Replace

Posted: Thu Mar 01, 2018 2:46 pm
by dunbarx
Hmmm.

Merge is one of those others. Likely the best one.

Craig

Re: Simple Parse and Replace

Posted: Thu Mar 01, 2018 7:36 pm
by jacque
I use merge exclusively for this type of thing. Just keep in mind that each variable name inside the double brackets has to match the variable name in the handler.

Re: Simple Parse and Replace

Posted: Thu Mar 01, 2018 9:48 pm
by andyt
Thanks everyone. I'll try out some of these suggestions this weekend and see how I get on.

Re: Simple Parse and Replace

Posted: Thu Mar 01, 2018 11:08 pm
by bwmilby
I think that the issue with your original RegEx is at least in part due to the fact that $ means the end of a line. To escape it you would need to use \$ instead.

Re: Simple Parse and Replace

Posted: Thu Mar 01, 2018 11:24 pm
by jameshale
Not only what Brian said but you only have a single $ at the start, not muliple.
Perhaps " \$%[a-Z]+%\$ " note there is a space before the first \$ and one after the second (this tells the opening one from the trailing one, depending on how you want to use the match.)

Using the merge idea, and replacing the ‘ $%’ with ‘ [[‘ and the ‘$% ‘ with ‘]] ‘ might be much more flexible though.