Need help modifying my Custom Replace code based on string passed to it
Posted
by fraXis
on Stack Overflow
See other posts from Stack Overflow
or by fraXis
Published on 2010-05-25T08:37:19Z
Indexed on
2010/05/25
8:41 UTC
Read the original article
Hit count: 487
Hello,
I have a C# program that will open a text file, parse it for certain criteria using a RegEx statement, and then write a new text file with the changed criteria.
For example:
I have a text file with a bunch of machine codes in it such as:
X0.109Y0Z1.G0H2E1
My C# program will take this and turn it into:
X0.109Y0G54G0T3
G43Z1.H2M08
(Note: the T3 value is really the H value (H2 in this case) + 1). T = H + 1
It works great, because the line usually always starts with X so the RegEx statement always matches.
My RegEx that works with my first example is as follows:
//Regex pattern for:
//- X(value)Y(value)Z(value)G(value)H(value)E(value)
//- X(value)Y(value)Z(value)G(value)H(value)E(value)M(value)
//- X(value)Y(value)Z(value)G(value)H(value)E(value)A(value)
//- X(value)Y(value)Z(value)G(value)H(value)E(value)M(value)A(value)
//value can be positive or negative, integer or floating point number with multiple decimal places or without any
private Regex regReal = new Regex("^(X([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]*){1}(Y([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]*){1}(Z([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]*){1}(G([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]*){1}(H([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]*){1}(E([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]*){1}(M([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]*)?(A([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]*)?$");
This RegEx works great because sometimes the line of code could also have an M or A at the end such as:
X0.109Y0Z1.G0H2E1A2
My problem is now I have run into some lines of code that have this:
G90G0X1.5Y-0.036E1Z3.H1
and I need to turn it into this:
G90G0X1.5Y-0.036G54T2
G43Z3.H1M08
Can someone please modify my RegEx and code to turn this:
G90G0X1.5Y-0.036E1Z3.H1
into:
G90G0X1.5Y-0.036G54T2
G43Z3.H1M08
But sometimes the values could be a little different such as:
G(value)G(value)X(value)Y(value)E(value)Z(value)H(value) G(value)G(value)X(value)Y(value)E(value)Z(value)H(value)A(value) G(value)G(value)X(value)Y(value)E(value)Z(value)H(value)A(value)(M)value G(value)G(value)X(value)Y(value)E(value)Z(value)H(value)M(value)(A)value
But also (this is where Z is moved to a different spot)
G(value)G(value)X(value)Y(value)Z(value)E(value)H(value)
G(value)G(value)X(value)Y(value)Z(value)E(value)H(value)A(value)
G(value)G(value)X(value)Y(value)Z(value)E(value)H(value)A(value)(M)value
G(value)G(value)X(value)Y(value)Z(value)E(value)H(value)M(value)(A)value
Here is my code that needs to be changed (I did not include the open and saving of the text file since that is pretty standard stuff).
//Regex pattern for:
//- X(value)Y(value)Z(value)G(value)H(value)E(value)
//- X(value)Y(value)Z(value)G(value)H(value)E(value)M(value)
//- X(value)Y(value)Z(value)G(value)H(value)E(value)A(value)
//- X(value)Y(value)Z(value)G(value)H(value)E(value)M(value)A(value)
//value can be pozitive or negative, integer or floating point number with multiple decimal places or without any
private Regex regReal = new Regex("^(X([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]*){1}(Y([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]*){1}(Z([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]*){1}(G([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]*){1}(H([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]*){1}(E([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]*){1}(M([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]*)?(A([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]*)?$");
private string CheckAndModifyLine(string line)
{
if (regReal.IsMatch(line)) //Check the first Regex with line string
{
return CustomReplace(line);
}
else
{
return line;
}
}
private string CustomReplace(string input)
{
string returnValue = String.Empty;
int zPos = input.IndexOf("Z");
int gPos = input.IndexOf("G");
int hPos = input.IndexOf("H");
int ePos = input.IndexOf("E");
int aPos = input.IndexOf("A");
int hValue = Int32.Parse(input.Substring(hPos + 1, ePos - hPos - 1)) + 1; //get H number
//remove A value
returnValue = ((aPos == -1) ? input : input.Substring(0, aPos));
//replace Z value
returnValue = Regex.Replace(returnValue, "Z[-]?\\d*\\.*\\d*", "G54");
//replace H value
returnValue = Regex.Replace(returnValue, "H\\d*\\.*\\d*", "T" + hValue.ToString() + ((aPos == -1) ? String.Empty : input.Substring(aPos, input.Length - aPos)));
//replace E, or E and M value
returnValue = Regex.Replace(returnValue, "E\\d*\\.*\\d(M\\d*\\.*\\d)?", Environment.NewLine + "G43" + input.Substring(zPos, gPos - zPos) + input.Substring(hPos, ePos - hPos) + "M08");
return returnValue;
}
I tried to modify the above code to match the new line of text I am encountering (and split into two lines like my first example) but I am failing miserably.
Thanks so much.
© Stack Overflow or respective owner