Solved

Help With Advanced String Replacement

Posted on 2006-10-29
6
189 Views
Last Modified: 2010-08-05
Experts,

I am working on a project and need to replace a block of text within a string started and ending with a unquie character string. For example,

My string would look something like this printed out:

<!---
The First Comment
-->

<!---
The Second Comment
-->

<!---
The Third Comment
-->

I need to somehow parse that string finding the starting "<!--" all text within the "-->" and store in an array so then I can loop through the array calling the replace function to swap the text to a new string. Does this make sense? I would greatly appreciate any tips or suggestions!

~ C
0
Comment
Question by:clickclickbang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 350 total points
ID: 17829600
Hi clickclickbang;

This will parse your string.

using System.Text.RegularExpressions;

        string input = "Your string to parse";
      ArrayList al = new ArrayList();
      MatchCollection mc = Regex.Matches(input, @"<!---\r\n(.*?)\r\n-->");
      foreach( Match m in mc )
      {
            al.Add(m.Groups[1].Value);
      }

There will be 3 elements in the array list with these values.
                  
The First Comment
The Second Comment
The Third Comment


Fernando
0
 
LVL 6

Assisted Solution

by:der_jth
der_jth earned 150 total points
ID: 17829614
This is definitely easiest to do with some regular expressions. Play around with the following code:

---
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1 {
  class Program {
    static void Main(string[] args) {
      string input =
@"This is some text
<!--
  This is an sgml comment
-->
This is some other text
<!--
 This is another sgml comment
-->";

      string result =
        Regex.Replace(
          input,
          @"<!--(.+?)-->",
          EvaluateMatch,
          RegexOptions.Singleline
        );

      Console.WriteLine(result);
      Console.ReadLine();
    }

    private static string EvaluateMatch(Match m) {

      return
        "[[Replaced '" + m.Value.Trim() +
        "' at position " + m.Index + "]]";
    }
  }
}
0
 
LVL 1

Author Comment

by:clickclickbang
ID: 17844369
Hi Guys, thanks for your post. Here is my working example below:

        MatchCollection mc = Regex.Matches(s, @"<!--_wc_start_-->(.+?)<!--_wc_end_-->");

        foreach (Match m in mc)
        {
            Regex FindId = new Regex(@"<!--(.+?)-->");
            Match IdValue = FindId.Match(m.Groups[1].Value);

            Response.Write(IdValue.Value);
        }

I sort of hybrid'ed this out of your two posts. Fernando, for some reason when using your regex pattern it didn't find anything. When I removed the /n/d it worked ok...BUT...in the second regex pattern (Regex FindId) the match includes the <!-- & and the -->. Is there anyway to ONLY grab the values inside the two delimiters?

Thanks again for your help!

~ C

PS - You guys are the Regex experts, if there is an easier way of going about this feel free to fill me in!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17847756
Hi clickclickbang;

In my solution I do not have a /n/d within the pattern. Taking another look at the original question I notice that in one part of the question you have the start of the pattern as "<!---" and in the paragraph that followed you have "<!--". I had set up my pattern for the one with three of - and not the one with two and mostly the reason for it not working.

To your last post, the question, "Is there anyway to ONLY grab the values inside the two delimiters?"  If you are talking about this pattern, "<!--(.+?)-->" then Group[1] should already be grabbing everything after "<!--" but before "-->".

To be able to help you further I will need to see the actual input data and what it looks like and what exactly are you trying to capture.

Fernando
0
 
LVL 1

Author Comment

by:clickclickbang
ID: 17849154
Fernando, lol, thanks for pointing that out. It must have been really late when I was working on this and posting this question. All looks good now, thanks for both of your posts!

~ C
0
 
LVL 1

Author Comment

by:clickclickbang
ID: 17849192
Fernando and Der_jth, I posted another question which involves a more detailed pattern match. I flagged it as a Regex question, however the responses I have gotten do not involve Regex. If either of you wouldn't mind taking a look at it and give me your opinion, below is the link:

http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_22044460.html

Bascially this question was founded on the question started here, yet I felt a new question was needed due to the fact that it was out of the scope of this question.

Thanks again for your help thus far!

~ C
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question