regan at netmail.co.nz
Thu Jul 12 05:08:58 PDT 2007
> I've got another question regarding regexp:
> If my document is full of variables such as "##var1 " and I want to replace them, with say "<var>var1</var>" how would I go about doing that? The variables now are preceded with two pound signs and ended with a single space.
> Also, could you please explain how the regexp function that you give me works? I'm trying to understand these, but think I'm not grasping the concept.
I'm no regexp expert but I did something similar last night.
You can use std.regexp.sub, eg.
std.regexp.sub(document, r"##([^ ]+) ", r"<var>$1</var>", "gi");
document - your complete document, or part of it.
"gi" - global and case insensitive.
global means look for the string more than once
case insensitive is self explanatory
r"##([^ ]+) " - wysiwyg pattern:
## - look for # followed by #
( and ) - forms a group which can be pasted into result
[^ ]+ - 1 or more characters which are NOT space
- the pattern finishes with a space
r"<var>$1</var>" - wysiwyg format:
<var> - literal
$1 - replace with first group from pattern
</var> - literal
So, it should look through document for the pattern "##([^ ]+) " and
replace it with the format "<var>$1</var>"
I tested this with:
import std.regexp, std.stdio;
string document = "This is a test ##bob and ##fred went for a walk to
##place how happy I am";
string pattern = r"##([^ ]+) ";
string format = r"<var>$1</var>";
writefln(std.regexp.sub(document, pattern, format, "gi"));
Seems to work. :)
More information about the Digitalmars-d-learn