Solved

Help With Advanced String Replacement

Posted on 2006-10-29
6
183 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
  • 3
  • 2
6 Comments
 
LVL 62

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 62

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now