Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help With Advanced String Replacement

Posted on 2006-10-29
6
Medium Priority
?
190 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 64

Accepted Solution

by:
Fernando Soto earned 1400 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 600 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 64

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

688 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