Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 798877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:02:25+00:00 2026-05-14T23:02:25+00:00

The below code is from my other questions that I have asked here on

  • 0

The below code is from my other questions that I have asked here on SO. Everyone has been so helpful and I almost have a grasp with regards to RegEx but I ran into another hurdle.

This is what I basically need to do in a nutshell. I need to take this line that is in a text file that I load into my content variable:

X17.8Y-1.Z0.1G0H1E1

I need to do a wildcard search for the X value, Y value, Z value, and H value. When I am done, I need this written back to my text file (I know how to create the text file so that is not the problem).

X17.8Y-1.G54G0T2
G43Z0.1H1M08

I have code that the kind users here have given me, except I need to create the T value at the end of the first line, and use the value from the H and increment it by 1 for the T value. For example:

X17.8Y-1.Z0.1G0H5E1

would translate as:

X17.8Y-1.G54G0T6
G43Z0.1H5M08

The T value is 6 because the H value is 5.

I have code that does everything (does two RegEx functions and separates the line of code into two new lines and adds some new G values). But I don’t know how to add the T value back into the first line and increment it by 1 of the H value. Here is my code:

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

  content = Regex.Replace(content, @"X[-\d.]+Y[-\d.]+", "$0G54G0");
  content = Regex.Replace(content, @"(Z(?:\d*\.)?\d+)[^H]*G0(H(?:\d*\.)?\d+)\w*", "\nG43$1$2M08"); //This must be created on a new line

This code works great at taking:

X17.8Y-1.Z0.1G0H5E1

and turning it into:

X17.8Y-1.G54G0
G43Z0.1H5M08

but I need it turned into this:

X17.8Y-1.G54G0T6
G43Z0.1H5M08

(notice the T value is added to the first line, which is the H value +1 (T = H + 1).

Can someone please modify my RegEx statement so I can do this automatically? I tried to combine my two RegEx statements into one line but I failed miserably.

Update1: Stephen in the comments below suggests, “there’s no arithmetic operators in regex, you’ll need to use a group to pull out the H value, turn it into an int, add one and build a new string.”. But I have no idea on how to do this in C# code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:02:26+00:00Added an answer on May 14, 2026 at 11:02 pm

    The easiest way to do this is with a simple program that uses a few Regex patterns that capture (named) groups, I had a little spare time so here you go:

    Program.cs

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                const string InputFileName = @"input.txt";
                const string OutputFileName = @"output.txt";
    
                List<Line> parsedLineList = new List<Line>();
    
                using (StreamReader sr = new StreamReader(InputFileName))
                {
                    string inputLine;
                    int lineNum = 0;
    
                    while ((inputLine = sr.ReadLine()) != null)
                    {
                        lineNum++;
    
                        Line parsedLine = new Line(inputLine);
    
                        if (parsedLine.IsMatch)
                        {
                            parsedLineList.Add(parsedLine);
                        }
                        else
                        {
                            Debug.WriteLine("Line {0} did not match pattern {1}", lineNum, inputLine);
                        }
                    }
                }
    
                using (StreamWriter sw = new StreamWriter(OutputFileName))
                {
                    foreach (Line line in parsedLineList)
                    {
                        sw.WriteLine(line.ToString());
                    }
                }
            }
        }
    }
    

    With input.txt containing:

    X17.8Y-1.Z0.1G0H1E1

    this program creates output.txt containing:

    X17.8Y-1.G54G0T2
    G43Z0.1H1M08

    The above code in Program.cs requires the following simple Line and Fragment class definitions:

    Line.cs

    namespace Fragments
    {
        class Line
        {
            private readonly static Regex Pattern =
                new Regex(@"^(?<X>X[^Y]+?)(?<Y>Y[^Z]+?)(?<Z>Z[^G]+?)(?<G>G[^H]+?)(?<H>H[^E]+?)(?<E>E[^$])$");
    
            public readonly string OriginalText;
    
            public string Text
            {
                get
                {
                    return this.X.ToString() + this.Y.ToString() + this.G54.ToString() + this.G.ToString() + this.T.ToString() + Environment.NewLine +
                           this.G43.ToString() + this.Z.ToString() + this.H.ToString() + this.M08.ToString();
                }
            }
    
            public readonly bool IsMatch;
    
            public Fragment X { get; set; }
            public Fragment Y { get; set; }
            public readonly Fragment G54 = new Fragment("G54");
            public Fragment G { get; set; }
            public Fragment T { get; set; }
            public readonly Fragment G43 = new Fragment("G43");
            public Fragment Z { get; set; }
            public Fragment H { get; set; }
            public readonly Fragment M08 = new Fragment("M08");
            public Fragment E { get; set; }
    
            public Line(string text)
            {
                this.OriginalText = text;
                Match match = Line.Pattern.Match(text);
                this.IsMatch = match.Success;
    
                if (match.Success)
                {
                    this.X = new Fragment(match.Groups["X"].Value);
                    this.Y = new Fragment(match.Groups["Y"].Value);
                    this.G = new Fragment(match.Groups["G"].Value);
                    this.Z = new Fragment(match.Groups["Z"].Value);
                    this.H = new Fragment(match.Groups["H"].Value);
                    this.E = new Fragment(match.Groups["E"].Value);
    
                    this.T = new Fragment('T', this.H.Number + 1.0);
                }
            }
    
            public override string ToString()
            {
                return this.Text;
            }
        }
    }
    

    Fragment.cs

    namespace Fragments
    {
        class Fragment
        {
            private readonly static Regex Pattern =
                new Regex(@"^(?<Letter>[A-Z]{1})(?<Number>.+)$");
    
            public readonly string Text;
            public readonly bool IsMatch;
    
            public readonly char Letter;
            public readonly double Number;
    
            public Fragment(string text)
            {
                this.Text = text;
                Match match = Fragment.Pattern.Match(text);
                this.IsMatch = match.Success;
    
                if (match.Success)
                {
                    this.Letter = match.Groups["Letter"].Value[0];
                    string possibleNumber = match.Groups["Number"].Value;
    
                    double parsedNumber;
                    if (double.TryParse(possibleNumber, out parsedNumber))
                    {
                        this.Number = parsedNumber;
                    }
                    else
                    {
                        Debug.WriteLine("Couldn't parse double from input {0}", possibleNumber);
                    }
                }
                else
                {
                    Debug.WriteLine("Fragment {0} did not match fragment pattern", text);
                }
            }
    
            public Fragment(char letter, double number)
            {
                this.Letter = letter;
                this.Number = number;
                this.Text = letter + number.ToString();
                this.IsMatch = true;
            }
    
            public override string ToString()
            {
                return this.Text;
            }
        }
    }
    

    Create a new C# Console Application project, add these three files, update your using statements and you’re ready to go. You can very easily alter the code in Program.cs to read the input and output filenames from Main’s command line arguments to make the program reusable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to pull some information from my tnsnames file using regex. I
I'm trying to build a C++ extension for python using swig. I've followed the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.