I used two separate dictionaries to associate the source and column fields, as I thought the associations are between source and column fields only. The sample code is made for a button that replaces the text of a label when it is clicked, but it can be adapted to any similar situation. So far this is what I came up with:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace RegexTest
{
public partial class Form1 : Form
{
Dictionary<string, string> values = new Dictionary<string, string>();
Dictionary<string, string> columns = new Dictionary<string, string>();
public Form1()
{
InitializeComponent();
InitValues();
}
private void InitValues()
{
values.Add("Source1", "ABC");
values.Add("Source2", "DEF");
values.Add("Source3", "GHI");
columns.Add("Column1", "Col1");
columns.Add("Column2", "Col2");
columns.Add("Column3", "Col3");
}
private void button1_Click(object sender, EventArgs e)
{
// Create the pattern
string pattern = "[a-z1-9]+\\[Source[0-9]+\\]\\.\\[[a-z1-9]+:Column[0-9]+:[a-z1-9]+\\][a-z1-9]+";
// Create a Regex
Regex rg = new Regex(pattern);
// Get all matches
MatchCollection matchedValues = rg.Matches(label1.Text);
StringBuilder sb = new StringBuilder();
// Replace all matches
for (int count = 0; count < matchedValues.Count; count++)
{
//copy the anytext part until the source
sb.Append(matchedValues[count].Value.Substring(0, matchedValues[count].Value.IndexOf('[')));
//replace the Source parts
sb.Append(values[matchedValues[count].Value.Substring(matchedValues[count].Value.IndexOf('[') + 1,
matchedValues[count].Value.IndexOf(']') - matchedValues[count].Value.IndexOf('['))]);
//now copy in the same way the anytext after source
//split in the same way around the : and use the columns dictionary
//finally, replace the original string with the value from string builder
label1.Text = sb.ToString();
sb.Clear();
}
}
}
}
The other parts are done in a similar way (I only made it find the first part, the "source", for the column part it is the same). If you'll need further help please ask and I'll answer as soon as possible. I also assumed that the anytext parts can contain only alpha-numerical text, if other characters can be found there, I'll edit the regex pattern.