Top index Wirbel home

::replaced_tags

string.replaced_tags(string st, string end, function(tuple(string), string) repl) - search and replace tags in string
string.replaced_tags(char st, char end, function(tuple(string), string) repl) - search and replace_tags in string, with tags being singled characters

string.replaced_tags(string st, string end, function(tuple(string), string) repl) searches a string for tags and replace them. The tags begin with st and end with end, both of which must be non-empty strings. For each tag found the body of the tag (i.e. the text between st and end) is converted into another string by calling the conversion function repl. The result is copied to the result string instead of the tag. All other characters are copied to the result verbatim. The original string s is not changed. The replacements are not done recursively. That means that if in the replacement strings there does appear a tag, it will not replaced.

string.replaced_tags(char st, char end, function(tuple(string), string) repl) is a conviance function that assumes the tags to be single characters

Examples

The following example replaces double-quoted substrings in the string text with ...:

   text.replaced_tags('"', '"', lambda tag: "" + tag + "")
Here is another example that looks in a string for Makefile-like variable-references ala $(VARNAME) and replaces them with values taken from a string to string dictionary:
   vars = { "NAME"  : "Max",
            "FROM"  : "Rosenheim",
            "WANTS" : "Friends" }
   print("Here is $(NAME) out of $(FROM) who wants $(WANTS).".
          replaced_tags("$(", ")", lambda t: vars[t]))
This prints: Here is Max out of Rosenheim who wants Friends.