0

I have a situation that a text file has lot of strings like shown below. I need to search for these pattern and replace the source and column codes with values. How can we do this string pattern search and replace in c# please? Thanks.

actual text: "anytext[Source1].[anytext:Column1:anytext]anytext"

updated text: "anytext[ABC].[anytext:Col1:anytext]anytext"

The code and value combinations look like below.

SourceCode ColumnCode Sourcevalue ColumnValue

====== ======== ========== ==========

Source1 Column1 ABC Col1

Source2 Column2 DEF Col2

Source3 Column3 GHI Col3

10
  • Could you please provide the structure of such a code (an example) and an example of a value you want it replaced with? It seems like Regex would do the job for you but we need at least the structure of the code fields Commented Jul 10, 2020 at 11:36
  • Did you want to know what is Code1/Code2 and replace with values from some kind of Dictionary or Code1 and Code2 are static strings? Commented Jul 10, 2020 at 11:37
  • Well I don't think it actually matters where he gets the values from (where the associations are stored), he stated that he wants to find and replace those "codes", but for that, in order to use Regex at least, we need the structure of the "codes" Commented Jul 10, 2020 at 11:39
  • @GabrielStancu Thanks. I've edited the question with example values added. So I have source and column codes in a table and I have to find the combinations in the string pattern and replace them with values. Commented Jul 10, 2020 at 11:44
  • I am not sure what you meant with the edit. The line above the ====== are the codes to be replaced by values from the rows under it? Commented Jul 10, 2020 at 11:56

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the help.I was able to get the code working and posted it as well for reference.
0

I wouldn't provide a complete working code that you copy & paste it without learning. Instead, I'll explain what you need to do step by step so you'll be able to write the code yourself. Remember, Stackoverflow isn't a code writing service.

The solution provided here is based on your comment:

the column code (ex Column1) can appear for more than one source code.

  1. Create a dictionary, let the key be a tuple that holds SourceCode and ColumnCode, and the value be a tuple that holds SourceValue and ColumnValue.

  2. Assuming that each line of the file is always in the format of SourceCode ColumnCode Sourcevalue ColumnValue, I'd read the file line by line, split it to an array of four strings (let's call the array splitted), Add tuple (splitted[0], splitted[1]) (key) and (splitted[2], splitted[3] (value) to the dictionary.

  3. Now, you have a dictionary representing the file content with O(1) access.

  4. Let's make the second assumption that your input string is in the format anytext[Source1].[anytext:Column1:anytext]anytext. I'd use Regex to get Source1 and Column1 from the string, then get the corresponding values from the dictionary. And finally do the replace.

1 Comment

Thanks. I followed your suggested approach and got it working with Regex! I will post the code as well for reference.
0

Just posting the final code that I got it working with the approach suggested by @Youssef13

Dictionary<Tuple<string, string>,Tuple<string,string>> sourcecolumncodeandvalue = new Dictionary<Tuple<string, string>, Tuple<string, string>>();
            sourcecolumncodeandvalue.Add(Tuple.Create("Source1", "Column1"), Tuple.Create("ABC", "Col1"));
            sourcecolumncodeandvalue.Add(Tuple.Create("Source2", "Column2"), Tuple.Create("DEF", "Col2"));

            Dictionary<string, string> codeandvaluereplacementlist = new Dictionary<string, string>();

            var pattern = @"\[(.*?)\]\.\[(.*?)\]";
            var filetext = "anytext[Source1].[anytext:Column1:anytext]anytext anytext[Source2].[anytext:Column2:anytext]anytext";
            var matchesfound = System.Text.RegularExpressions.Regex.Matches(filetext, pattern); //find the pattern [].[]
            foreach (System.Text.RegularExpressions.Match  m in matchesfound)
            {
                string datasource = string.Empty;
                string columnname = string.Empty;
                string replacementtext = string.Empty;

                string[] sourceandcolumnsplit = m.Value.ToString().Split('.');//split [].[] into two based on '.' character
                datasource = sourceandcolumnsplit[0].Replace("[","").Replace("]",""); //remove square brackets               
                //Column value is in between ':' character (ex: anytext:Column2:anytext)  so split it further 
                string[] columnsplit = sourceandcolumnsplit[1].Split(':');
                columnname = columnsplit[1];
                //We got the source and column codes, now get corresponding values from the dictionary
                Tuple<string,string> sourceandcolumnvalues;
                sourcecolumncodeandvalue.TryGetValue(Tuple.Create(datasource, columnname),out sourceandcolumnvalues);

                //construct the replacement value string for each code string
                codeandvaluereplacementlist.Add(m.Value.ToString(), "[" + sourceandcolumnvalues.Item1 + "]." + columnsplit[0] + ":" + sourceandcolumnvalues.Item2 + ":" + columnsplit[2]);
            }
            //Finally loop through all code matches and replace with values in the file text
            foreach (var codeandvalue in codeandvaluereplacementlist)
            {
                filetext = filetext.Replace(codeandvalue.Key, codeandvalue.Value);
            }

Comments

-1
var source = "anytext[Source1].[anytext:Column1:anytext]anytext";
var src1 = "Source1";
var dest1 = "ABC";
var src2 = "Column1";
var dest2 = "Col1";

var result = source
                .Replace("[" + src1 + "]", "[" + dest1 +"]")
                .Replace(":" + src2 + ":", ":" + dest2 +":");

https://dotnetfiddle.net/5cRnYD

Of course you may use any list/dictionary/file for src and dest values.

1 Comment

Thanks, But the source I have is a huge text which may have the pattern "[Source1].[anytext:Column1:anytext]" several times with. So I wanted a regex to find out each of those patterns with in the large text and then do a replace option only on those pattern string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.