Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

Help With Advanced String Replacement

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
clickclickbang
Asked:
clickclickbang
  • 3
  • 2
2 Solutions
 
Fernando SotoCommented:
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
 
der_jthCommented:
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
 
clickclickbangAuthor Commented:
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
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.

 
Fernando SotoCommented:
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
 
clickclickbangAuthor Commented:
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
 
clickclickbangAuthor Commented:
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

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.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now