Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find & Replace, Variables in text questions
#3
i.
Simplest replacement
Code:
Copy      Help
str template en fn

;lets create template text for testing. Normally you would probably use template.getfile("...").
template=
;...
;... <a href="http://nonprofit.org/mediafiles/{FN}">{EN}</a> ...
;...


;set fn and en
fn="filename.jpg"
en="right-click to download"

;replace
template.findreplace("{FN}" fn)
template.findreplace("{EN}" en)

out template

;{EN} and {FN} here used as examples of placeholders for variables in the template.

Replacement using regular expressions
Code:
Copy      Help
str template en fn

;lets create template text for testing. Normally you would probably use template.getfile("...").
template=
;...
;... <a href="http://nonprofit.org/mediafiles/{FN}">{EN}</a> ...
;...


;set fn and en
fn="filename.jpg"
en="right-click to download"

;Replace text in template. Use regular expression.

;to make more clear, I at first store the regular expression to rx variable and give some explanations
str rx="(<a href=''http://nonprofit.org/mediafiles/)\{FN\}''>\{EN\}</a>"
;\;- used to escape { and } because they are special characters in regex syntax
;() - used to mark parts of text
;Here '' is used for ". It is not part of regular expression syntax.


;the replacement must contain variables, therefore we need to format it at first
str repl.format("$1%s''>%s</a>" fn en) ;;replaces %s to values of fn and en. Regex specific: $1 represents the enclosed part

;now replace
template.replacerx(rx repl 1)

out template

;In real world, the regular expression should be more sophisticated, eg use \s instead of space (because it may be line break)

;{EN} and {FN} here used as examples of placeholders for variables in the template.

ii.
Hope the above will help with it.


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)