views:

27

answers:

1

I have a string that I want to extract text between comment tags, manipulate it, and replace it back. Here is what I am trying to work with:

...
<!--RegionStart url="http://domain1.com"--&gt;
    some text here
<!--RegionFinish-->
...
<!--RegionStart url="http://domain2.com"--&gt;
    some text there
<!--RegionFinish-->
...
<!--RegionStart url="http://domain3.com"--&gt;
    some text anywhere
<!--RegionFinish-->
...

I would like to get a collection of the text between the comment tags and enumerate thru them like this:

foreach (string item in collection)
{
    string newText = item.Replace("some", "all") + Custom(url);
    //put text back somehow
}

I am trying to end up with this:

...
<!--RegionStart url="http://domain1.com"--&gt;
    all text here domain1.com
<!--RegionFinish-->
...
<!--RegionStart url="http://domain2.com"--&gt;
    all text there domain2.com
<!--RegionFinish-->
...
<!--RegionStart url="http://domain3.com"--&gt;
    all text anywhere domain3.com
<!--RegionFinish-->
...

How can I do this?

+2  A: 

The main thing understand here are

  1. Non-greedy regex matching (*? etc) is needed
  2. It's probably easiest to use Replace with a custom MatchEvaluator.

This should do the trick:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        outputTextBox.Text = "";
        Regex regex = new Regex("(<!--RegionStart url=\"http://(.*?)\"--&gt;)(.*?)(&lt;!--RegionFinish--&gt;)", RegexOptions.Singleline);

        string copy = inputTextBox.Text;
        MatchCollection coll = regex.Matches(inputTextBox.Text);                
        outputTextBox.Text = regex.Replace(copy, new MatchEvaluator(Replace));
    }

    public string Replace(Match m)        
    {
        // Format the text you want to get back:
        return String.Format("{0}{1} {2}{3}", 
            m.Groups[1].ToString() + Environment.NewLine, 
            m.Groups[3].ToString().Replace("some", "all").Trim(),
            m.Groups[2].ToString().Trim() + Environment.NewLine, 
            m.Groups[4].ToString());
    }
}
steinar
Brilliant!!!!!!
TruMan1