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?
thanks in advnace for any help,
d
protected string RegExpReplace(string input,string pattern,string
replacement)
{
string output;
System.Text.RegularExpressions.Regex re=new
System.Text.RegularExpressions.Regex(pattern,System.Text.RegularExpressions.RegexOptions.Multiline);
output=re.Replace(input,replacement);
return output;
}
and use it as
strTmp=RegExpReplace(strTmp,", ([A-Z]{2}) (\\d{5})","_$1_$2");
Cheers,
JP
------------------------------------------------------------------
A program is a device used to convert,
data into error messages
------------------------------------------------------------------
"D" <daa...@gmail.com> wrote in message
news:1125417416.8...@g43g2000cwa.googlegroups.com...
>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?
Use this regex: (?<= ^HOST=).+(?= ;[^;]*$)
And replace by the string you want to use, without any modification.
Calling Regex like this should do it:
string newString = Regex.Replace(@"(?<= ^HOST=).+(?= ;[^;]*$)", strReplace);
Docs are here (MSDN): http://shrinkster.com/7pe and here:
http://shrinkster.com/7pf
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
thanks for all your help.
don
>this seems a bit backwards... that i would have to package a new
>regular expression to group everything but what i wanted to replace,
>and then reconcatenate . so is the consensus ? there is no way to do
>a simple replace of $1 ???
I'm not quite following you. The expression doesn't "package everything",
it only uses a lookbehind and a lookahead assertion to make sure it has
found the right place in the string. I don't think it's possible in the
.NET regex implementation to replace only a specific capture group in a
string - I may be wrong. Admitted, my Perl may be a little rusty, but how
do you do that in Perl?
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/