Simple Parse and Replace

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
andyt
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 15
Joined: Mon Mar 08, 2010 10:41 am

Simple Parse and Replace

Post by andyt » Wed Feb 28, 2018 11:31 pm

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?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Simple Parse and Replace

Post by dunbarx » Thu Mar 01, 2018 12:15 am

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

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Simple Parse and Replace

Post by [-hh] » Thu Mar 01, 2018 2:55 am

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.
shiftLock happens

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Simple Parse and Replace

Post by dunbarx » Thu Mar 01, 2018 2:46 pm

Hmmm.

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

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Simple Parse and Replace

Post by jacque » Thu Mar 01, 2018 7:36 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

andyt
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 15
Joined: Mon Mar 08, 2010 10:41 am

Re: Simple Parse and Replace

Post by andyt » Thu Mar 01, 2018 9:48 pm

Thanks everyone. I'll try out some of these suggestions this weekend and see how I get on.

bwmilby
Posts: 438
Joined: Wed Jun 07, 2017 5:37 am
Location: Henrico, VA
Contact:

Re: Simple Parse and Replace

Post by bwmilby » Thu Mar 01, 2018 11:08 pm

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.
Brian Milby

Script Tracker https://github.com/bwmilby/scriptTracker

jameshale
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 474
Joined: Thu Sep 04, 2008 6:23 am
Location: Melbourne Australia

Re: Simple Parse and Replace

Post by jameshale » Thu Mar 01, 2018 11:24 pm

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.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”