Connecting Tech Pros Worldwide Forums | Help | Site Map

regex replace question

=?Utf-8?B?TWFya19C?=
Guest
 
Posts: n/a
#1: Sep 5 '08
I am trying to replace any occurance of minus-point "-." with
minus-zero-point "-0."

My regex: Regex repoint_num = new Regex(@"\-\.");

My data: string xyz = "-.9";

My command: repoint_num.Replace(expr, "0.");

The match is detected but the replace does not occur.

I'm stupid I know, but how do I correct this?





=?Utf-8?B?TWFya19C?=
Guest
 
Posts: n/a
#2: Sep 5 '08

re: regex replace question


We have these moments here at Golden Acres:


My (new) command: expr = repoint_num.Replace(expr, "0.");



"Mark_B" wrote:
Quote:
I am trying to replace any occurance of minus-point "-." with
minus-zero-point "-0."
>
My regex: Regex repoint_num = new Regex(@"\-\.");
>
My data: string xyz = "-.9";
>
My command: repoint_num.Replace(expr, "0.");
>
The match is detected but the replace does not occur.
>
I'm stupid I know, but how do I correct this?
>
>
>
>
Peter Duniho
Guest
 
Posts: n/a
#3: Sep 5 '08

re: regex replace question


On Fri, 05 Sep 2008 12:16:11 -0700, Mark_B
<MarkB@discussions.microsoft.comwrote:
Quote:
I am trying to replace any occurance of minus-point "-." with
minus-zero-point "-0."
>
My regex: Regex repoint_num = new Regex(@"\-\.");
>
My data: string xyz = "-.9";
>
My command: repoint_num.Replace(expr, "0.");
>
The match is detected but the replace does not occur.
>
I'm stupid I know, but how do I correct this?
Impossible to say if you don't post the code you're using, and you
obviously haven't done that.

But, based on the narrative, I'd say there's a possibility you're
expecting the string referenced by "xyz" to change, which won't work.
Strings are immutable. The Replace() method returns a new string, and
that's the one you should use if you want the altered value.

Alternatively, the code you posted is closer to your actual code that one
might hope, in which case at least part of the problem is that you're
passing "expr" to the Replace() method when you really want to search the
string referenced by "xyz".

By the way, if you want "-." to be replaced by "-0.", why isn't your
replacement string "-0."?

If the above doesn't clear things up, you really should post the actual
code. Psychic debugging can take us only so far.

Pete
Closed Thread


Similar C# / C Sharp bytes