rchar[] replace(rchar[] string, rchar[] format)
|
Finds regular expression matches in string and replaces those matches with a new string composed of format merged with the result of the matches.
If the multiple line attribute is set, ^ and $ represent the start and end of a line. If the multiple line attribute is not set, ^ and $ represent the start and end of the entire string.
If the global attribute is set, replace all matches. Otherwise, replace the first match.
Example:
import std.stdio;
import std.regexp;
int main() {
char[] contacts =
"Joe: joe@asdf.com\n"
~"Sarah: sarah@asdf.com\n"
~"Rob: rob@asdf.com";
char[] simple_email_pattern = "([a-z]+@[a-z]+\\.[a-z]+)";
char[] mailto = "<a href=\"mailto:$1\">$1</a>";
RegExp reg = new RegExp(simple_email_pattern,"gi"); char[] html = reg.replace(contacts,mailto);
reg.compile("^([a-z]+)","gmi"); html = reg.replace(html,"<b>$1</b>");
reg.compile("^(.+)$","gm"); html = reg.replace(html,"<p>$1</p>\n");
writefln("%s",html);
return 0;
}
|
Output:
<p><b>Joe</b>: <a href="mailto:joe@asdf.com">joe@asdf.com</a></p>
<p><b>Sarah</b>: <a href="mailto:sarah@asdf.com">sarah@asdf.com</a></p>
<p><b>Rob</b>: <a href="mailto:rob@asdf.com">rob@asdf.com</a> |
|