Using Regex.Replace to save characters that may change

I have the following:

string text = "version=\"1,0\"";

I want to replace commawith dot, keeping 1 and 0, BUT bearing in mind that they are different in different situations! It could be version="2,3".

Smart ass and noob-unworking way to do this:

           for (int i = 0; i <= 9; i++)
            {
                for (int z = 0; z <= 9; z++)
                {
                    text = Regex.Replace(text, "version=\"i,z\"", "version=\"i.z\"");
                }
            }

But, of course, is a string, and I do not want to i, and zacted like a line there.

I could also try the lame one, but work:

text = Regex.Replace(text, "version=\"1,", "version=\"1.");
text = Regex.Replace(text, "version=\"2,", "version=\"2.");
text = Regex.Replace(text, "version=\"3,", "version=\"3.");

And so on .. but that would be lame.

Any tips on how to deal with this alone?

Edit: I have other commas that I don't want to replace, so text.Replace(",",".")I cannot do

+4
source share
3 answers

,

Regex reg = new Regex("(version=\"[0-9]),([0-9]\")");

:

text = reg.Replace(text, "$1.$2");

$1, $2 .., .

+5
(?<=version=")(\d+),

. . $1.

https://regex101.com/r/sJ9gM7/52

+2

, :

string text = "version=\"1,0\"";
var regex = new Regex(@"version=""(\d*),(\d*)""");
var result = regex.Replace(text, "version=\"$1.$2\"");

, , ( , ), $1 $2 .


, , .. version="1,1,0". , "" . , , ( # dev, , , :)):

private static string SpecialReplace(string text)
{
    var result = text.Replace(',', '.');
    return result;
}
public static void Main()
{
    string text = "version=\"1,0,0\"";
    var regex = new Regex(@"version=""[\d,]*""");
    var result = regex.Replace(text, x => SpecialReplace(x.Value));
    Console.WriteLine(result);
}

version="1.0.0".

"version=""[\d,]*""" version="...", .

, , SpecialReplace, .

ideone

+1
source

All Articles