Simple Parse and Replace
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Simple Parse and Replace
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?
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?
-
- VIP Livecode Opensource Backer
- Posts: 9648
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Simple Parse and Replace
Hi. If I understand you correctly, say you have some text in fld 1, like:
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):
There are others...
Craig Newman
You might do it this way:Now is the time $%adam$% for all good men $%Eve$% to come to the aid $%Snake$% of their country..
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
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
Craig Newman
-
- VIP Livecode Opensource Backer
- Posts: 2262
- Joined: Thu Feb 28, 2013 11:52 pm
- Location: Göttingen, DE
Re: Simple Parse and Replace
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.
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
-
- VIP Livecode Opensource Backer
- Posts: 9648
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Simple Parse and Replace
Hmmm.
Merge is one of those others. Likely the best one.
Craig
Merge is one of those others. Likely the best one.
Craig
-
- VIP Livecode Opensource Backer
- Posts: 7229
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Simple Parse and Replace
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
HyperActive Software | http://www.hyperactivesw.com
Re: Simple Parse and Replace
Thanks everyone. I'll try out some of these suggestions this weekend and see how I get on.
Re: Simple Parse and Replace
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
Script Tracker https://github.com/bwmilby/scriptTracker
-
- VIP Livecode Opensource Backer
- Posts: 474
- Joined: Thu Sep 04, 2008 6:23 am
- Location: Melbourne Australia
Re: Simple Parse and Replace
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.
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.