Find and replace RegEx query

I am starting to capture RegEx thanks to all the great help here on SO with my other questions. But I still suck this one:

My code is:

   StreamReader reader = new StreamReader(fDialog.FileName.ToString());
   string content = reader.ReadToEnd();
   reader.Close();


I am reading a text file and I want to search for this text and change it (the X and Y values ​​always follow each other in the text file):

X17.8Y-1.

But this text can also be X16.1Y2.3 (the values ​​will always be different after X and Y)


I want to change it to

X17.8Y-1.G54
or
<b> X (value) Y (value) G54



My RegEx instruction follows, but it does not work.

content = Regex.Replace(content, @"(X(?:\d*\.)?\d+)*(Y(?:\d*\.)?\d+)", "$1$2G54");


Can someone change it for me to work and look for X (wildcard) Y (wildcard) and replace it with X (value) Y (value) G54?

+3
5

, :

X[-\d.]+Y[-\d.]+

#:

string content = "foo X17.8Y-1. bar";
content = Regex.Replace(content, @"X[-\d.]+Y[-\d.]+", "$0G54");
Console.WriteLine(content);

:

foo X17.8Y-1.G54 bar
+2

,

string num = @"-?(?:\d+\.\d+|\d+\.|\.\d+|\d+)";
content = Regex.Replace(content, "(?<x>X" + num + ")(?<y>Y" + num + ")", "${x}${y}G54");

Y? , :

content = Regex.Replace(content, @"(X.+?)(Y.+?)(\s)", "$1$2G54$3");

? - , .

+1

"X () Y ()" ? ? ? "X () Y ()"?

, \d, .NET, :

(X[0-9.-]+Y[0-9.-]+)

$1G54

, , .

+1

, , Expresso. .NET, , . .

, , - .

0

, :

content = Regex.Replace(content, @"(X-?(?:\d*\.)?\d+)*(Y-?(?:\d*\.)?\d+)", "$1$2G54");
0

Source: https://habr.com/ru/post/1744847/


All Articles