"D" <da*****@gmail.com> wrote in
news:11**********************@g43g2000cwa.googlegr oups.com:
hi there ,
i want to do something fairly simple (well it was simple in
PERL) using the replace function of Regex... but i cannot find
the docs to help me on it... i want to use a regex to find a
string:
^HOST=(.+);[^;]*$
and then replace group 1 (inside the .+) with another string...
say the variable strReplace.
can anyone show me how to do this easily?
d,
With that regex, one way to do this is to reverse the logic of the
regex. Used named capture on the parts of the regex you want to
keep, and replace the unnamed part.
Below is a regex that names the parts to keep as "left" and "right".
These named capture groups are used to construct the replacement
string:
string input = "HOST=1.2.3.4;some more text...";
string pattern = "^(?<left>HOST=).+(?<right>;[^;]*)$";
string replacement = "${left}new.host.name${right}";
Console.WriteLine(Regex.Replace(input, pattern, replacement));
A better solution would be to use a different regex. The regex
you're using will match an entire string. Ideally, you would want to
have a regex that only matches the text you want to replace. Does
the text betwee "HOST=" and ";" have any kind of pattern? If so, can
a regex be used to match just that text?
--
Hope this helps.
Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/