regexp-replace

Replace information in a string.

This replaces all occurrences of regexp in input with new-substring.  If count: is specified, it replaces only the first count occurrences of regexp.  (This is different from Perl, which replaces only the first occurrence unless /g is specified) New-substring can contain back-references to the regexp.  For instance,

regexp-replace("The rain in Spain and some other text",
"the (.*) in (\\w*\\b)", "\\2 has its \\1")

returns “Spain has its rain and some other text”.  If the subgroup referred to by the back-reference was not matched, the reference is interpreted as the null string.  For instance,

regexp-replace("Hi there", "Hi there(, Bert)?",
"What do you think\\1?")

returns “What do you think?” because “, Bert” wasn’t found.

Exported from

Arguments

inputAn instance of <string>.  The string to parse and replace pieces of.
regexpAn instance of <string>.
new-substringAn instance of <string>.  The replacement string.
count:An instance of <integer> or #f.  If supplied, number of substitutions to make.  The default is #f.
case-sensitive:An instance of <object>.  If true, match case in regexp while parsing.  The default is #f.
start:An instance of <integer>.  Where to start parsing the string.  The default is 0.
end:An instance of <integer> or #f.  If defined, where to stop parsing the string.  The default is #f.

Values

changed-stringAn instance of <string>.
Parsing a regexp is not cheap, so we cache the parsed regexps and only parse a string if we haven’t seen it before.
The class of sequences with elements that are characters.
The class of integers.
The class of all Dylan objects.