Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone
I want to replace text Between Two words in Text File, I want to always replace text Between Two words with Some Text.
(between two words there can be different text)
For example:
First word: "text1"
Second word: "text2"
between to always replace with: "some text"

Before executing code:
"text1"..blah...blah..123..."text2"
Or
"text1"www.bla.com...{}..."text2"

After executing code to always be like this:
"text1"some text"text2"

I hope you understand...Please help.Thank you.

What I have tried:

C#
private void ReplaceBetween()
        {
            string[] fileLines = File.ReadAllLines(@"C:\WriteText.txt");

            for (int i = 0; i < fileLines.Length; i++)
            {
                int start = fileLines[i].IndexOf("text1");
                int end = fileLines[i].LastIndexOf("text2");
                if (start >= 0 && end >= 0)
                {
                    fileLines[i] = fileLines[i].Replace(everything between "text1" and "text2" with "some text");
                }
            }
            File.WriteAllLines(@"C:\WriteText.txt", fileLines);
        }
Posted
Updated 6-Oct-22 7:38am
v2

Don;t read it as separate lines: use File.ReadAllText[^] instead to get a single string containing the whole file.

Then use a Regex:
RegEx
(?<=text1).*?(?=text2)
If you look at the Regex.Replace method[^] it will do all the substitutions for you.

Then the File.WriteAllText method updates the file .
 
Share this answer
 
Comments
Member 10410972 6-Oct-22 4:28am    
Thanks for your reply, I tried to apply the Regex method but it doesn't work. I have no experience with Regex method...
I am asking for your help specifically in my example, if possible more detailed help... I apologize, thank you.
OriginalGriff 6-Oct-22 4:55am    
Actually, it does work. I tried it.

But I have no idea *what* you tried, or how it didn't work.

"It doesn't work" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.

Solution1 is best but, if you just want to use your approach, something like this may work.

C#
private static void ReplaceBetween()
{
    string replacedText = " some replaced text ";
    string[] fileLines = new[] {
       "Start text1..blah...blah..123...text2 hello",
       "something text1always carry an umbrellatext2 end",
       "nothing to be replaced here",
       "finally text1Mary had a little lambtext2 finish"
    };
    //  string[] fileLines = File.ReadAllLines(@"C:\WriteText.txt");
    string markS = "text1";
    string markE = "text2";
    for (int i = 0; i < fileLines.Length; i++)
    {
        string line = fileLines[i];
        int start = line.IndexOf(markS);
        int end = line.LastIndexOf(markE);

        if (start >= 0 && end >= 0)
        {
            int length = line.IndexOf(markE) + markE.Length - start;
            //include the markers in the substring
            //in case the substring occurs elsewhere without the markers
            var textToReplace = line.Substring(start, length);
            //add the markers to the relacement
            string replacement = markS + replacedText + markE;
            var result = line.Replace(textToReplace, replacement);
            fileLines[i] = result;
        }
    }
    //  File.WriteAllLines(@"C:\WriteText.txt", fileLines);
}

 
Share this answer
 
Comments
Member 10410972 6-Oct-22 15:59pm    
Thank you very much George, it works for me. Best regards.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900