0

I'm trying to create a find/replace regular expression in C# to find strings that start with -i, then contain any number of digits (no spaces) and then replace the -i with an empty string, but also add 2 spaces to the end to make up for removing the -i (it is a fix length file).

Right now, I am doing this to replace the text without adding the spaces at the end:

File.WriteAllText(textBox1.Text, Regex.Replace(File.ReadAllText(textBox1.Text), @"[-i]", ""));

An example line in the file is:

-i3598            00015

And I want the result to be:

3598              00015

Notice that the total length is the same before and after.

Thanks in advance!

1 Answer 1

3

Please try the following. It uses Regular Expressions Substitutions in the replacement pattern.

File.WriteAllText(
    textBox1.Text, 
    Regex.Replace(
        File.ReadAllText(textBox1.Text), 
        @"-i(\d+)", 
        "$1  "));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.