I had a need, so I scratched it.
Code:
/*
version: 2009-06-03v01
author: ScottMattes
purpose: to dynamically prompt the user for values to replace certain parts of a text string
Examples:
; substitutions with no defaults
raw_text := "<@now@> is the time for all good <@men@> to come to the aid of their <@country@>"
msg_text := text_substitue( raw_text )
msgbox, With substitution:`n`n%raw_text% `n`n%msg_text%
; substitutions with defaults
raw_text := "<@now:Noon@> is the time for all good <@men:employees@> to come to the aid of their <@country:microwave@>!"
msg_text := text_substitue( raw_text )
msgbox, With substitution:`n`n%raw_text% `n`n%msg_text%
; no substitutions needed
raw_text := "text with no substitution needed"
msg_text := text_substitue( raw_text )
msgbox, Without substitution:`n`n%raw_text% `n`n%msg_text%
*/
text_substitue( i_text )
{
OutputDebug, text_substitue: -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
OutputDebug, text_substitue: incoming i_text=%i_text%
startpos = 1
loop
{
OutputDebug, text_substitue: ---------------------
FoundPos := RegExMatch(i_text, "UP)<@(.*)@>", match, startpos)
ifequal, foundpos, 0
{
break ; there are no more to find, time to wrap things up and leave
}
else
{
varname := substr( i_text, matchpos1, matchlen1 )
OutputDebug, text_substitue: varname=%varname%
subval := varname
OutputDebug, text_substitue: subval=%subval%
startpos := matchpos1 + 1
OutputDebug, text_substitue: matchpos1=%matchpos1%
; if there is a default specified for the varname, use it
ifinstring, varname, :
{
stringsplit, var, varname, :
OutputDebug, text_substitue: varname=%varname% var1=%var1% var2=%var2%
varname := var1
vardefault := var2
OutputDebug, text_substitue: varname=%varname% vardefault=%vardefault%
}
inputbox, r_value, Variable replacement, %i_text%`n`nPlease enter a value for '%varname%':, , , , , , , , %vardefault%
ifequal, errorlevel, 0
{
StringReplace, i_text, i_text, <@%subval%@>, %r_value%
OutputDebug, text_substitue: r_value=%r_value%
}
else
{
OutputDebug, text_substitue: user clicked Cancel in InputBox
}
}
OutputDebug, text_substitue: i_text=%i_text%
}
OutputDebug, text_substitue: ---------------------
OutputDebug, text_substitue: outgoing i_text=%i_text%
return %i_text%
}
Thoughts, comments, suggestions, all are welcome.
EDIT: 2009-06-03v01
corrected startpos problem
added OutputDebug calls