Solved

Help With Advanced String Replacement

Posted on 2006-10-29
6
187 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

740 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