In the following code, the pattern is reading text between specified tags and I want to replace text in between those specified tags. I tried but replacing in whole file.

string input = File.ReadAllText(@"D:\text\div.txt");
            string pattern = @"<body.*?>(.|\n)*?</body>";//reads text between specified tags
            MatchCollection matches = Regex.Matches(input, pattern);
            // Console.WriteLine("Matches found: {0}", matches.Count);
            input = input.Replace("oldvalue", "newvalue");
            File.WriteAllText(@"D:\text\div.txt", input);
            if (matches.Count > 0)
                foreach (Match m in matches)
                Console.WriteLine(m.Groups[0]);
                Console.ReadLine();

plz help me
thanks in advance

Recommended Answers

All 5 Replies

do you really have to use regex for this?

i really don t know where all the people have gone so i must do something
here you go try this, it s not using regex, actually linq with lamda is powerfull with this but this is a simple solution to it

 static void Main(string[] args)
        {
            string html_tag = "";
            int position1 = 0;
            int position2 = 0;
            string insert = "";
            string text_to_put="Have a nice day";// whatever you want to replace the first string with

            string document_path=@"D:\text\div.txt";
            string input = "";
            string concatenate="";
            string pattern = @"<body.*?>(.|\n)*?</body>";//regex code, but not needed


            using (StreamReader str=new StreamReader(document_path))
            {

                while (!str.EndOfStream) 
                {
                    input = str.ReadLine();
                    concatenate=concatenate+input; //instead of regex
                    position1 = input.IndexOf('<');
                    position2 = input.IndexOf('>');


                    if (input.Contains("body")) 
                    {
                        for (int i = position1; i<=position2 ; i++)
                        {
                             html_tag = html_tag + input[i];
                        }
                    }
                    str.Close();
              }

            }
            concatenate=concatenate.Replace(concatenate,text_to_put);

            insert = html_tag.Insert(html_tag.IndexOf('>')+1,concatenate);

            Console.WriteLine(insert);

                  using (StreamWriter sw = new StreamWriter(document_path)) 
                  {
                      sw.Write(insert);
                      sw.Close();
                  }
            Console.ReadLine();

            //// we don t need this at the moment 
            //if (matches.Count > 0)
            //    foreach (Match m in matches)
            //        Console.WriteLine(m.Groups[0]);
            //Console.ReadLine();
        }

while (!str.EndOfStream)//getting error cannot read from a closed textreader

 static void Main(string[] args)
        {
            string html_tag = "";
            int position1 = 0;
            int position2 = 0;
            string insert = "";
            string text_to_put="Have a nice day";// whatever you want to replace the first string with
            string document_path=@"D:\text\div.txt";
            string input = "";
            string concatenate="";
            string pattern = @"<body.*?>(.|\n)*?</body>";//regex code, but not needed
            using (StreamReader str=new StreamReader(document_path))
            {
                while (!str.EndOfStream) 
                {
                    input = str.ReadLine();
                    concatenate=concatenate+input; //instead of regex
                    position1 = input.IndexOf('<');
                    position2 = input.IndexOf('>');
                    if (input.Contains("body")) 
                    {
                        for (int i = position1; i<=position2 ; i++)
                        {
                             html_tag = html_tag + input[i];
                        }
                    }

                 }
              str.Close();
            }
            concatenate=concatenate.Replace(concatenate,text_to_put);
            insert = html_tag.Insert(html_tag.IndexOf('>')+1,concatenate);
            Console.WriteLine(insert);
                  using (StreamWriter sw = new StreamWriter(document_path)) 
                  {
                      sw.Write(insert);
                      sw.Close();
                  }
            Console.ReadLine();
            //// we don t need this at the moment 
            //if (matches.Count > 0)
            //    foreach (Match m in matches)
            //        Console.WriteLine(m.Groups[0]);
            //Console.ReadLine();
        }

i m sorry, but you could have seen that i put the .close() inside the while loop which gives an error, just put it outside the while loop

The previous code will work but everything will be in the same line. If that bothers you then use this but still if retrieving a html i think that it shouldn t be problem but neverthless.

static void Main(string[] args)
        {
            string html_tag="";
            int position1 = 0;
            int position2 = 0;
            string insert = "";
            string text_to_put="Have a nice day";// whatever you want to replace the first string with

            string document_path=@"D:\text\div.txt";
            string input = "";
            string concatenate="";
            string pattern = @"<body.*?>(.|\n)*?</body>";//regex code, but not needed


            using (StreamReader str=new StreamReader(document_path))
            {

                while (!str.EndOfStream) 
                {
                    input = str.ReadLine();
                    concatenate=concatenate+input; //instead of regex
                    position1 = input.IndexOf('<');
                    position2 = input.IndexOf('>');


                    if (input.Contains("body")) 
                    {
                        for (int i = position1; i<=position2 ; i++)
                        {
                             html_tag = html_tag + input[i];
                        }
                        html_tag = html_tag + " ";
                    }

              }
                str.Close();

            }

            string [] array=new string[2];
            array = html_tag.Split(' ');
           concatenate=concatenate.Replace(concatenate,text_to_put);


            Console.WriteLine(insert);

                  using (StreamWriter sw = new StreamWriter(document_path)) 
                  {
                      sw.WriteLine(array[0]);
                      sw.WriteLine(concatenate);
                      sw.Write(array[1]);
                      sw.Close();
                  }
            Console.ReadLine();

            //// we don t need this at the moment 
            //if (matches.Count > 0)
            //    foreach (Match m in matches)
            //        Console.WriteLine(m.Groups[0]);
            //Console.ReadLine();
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.