Skip to main content

C# Regex replace all occurrences of 3 numbers together with a different one from a list

I have a list of numbers that have in the left the new number and in the right the old number I want to change, each separated by a Tab character:

000    256
007    002
056    078

And I have filenames with the following composition:

aaaa_bbb_01_cccc_000_a
aaaa_bbb_01_cccc_000_b
aaaa_bbb_01_cccc_000_c
aaaa_bbb_01_cccc_007_a
aaaa_bbb_01_cccc_056_a 

I want to change the 3 digits that are together into it's corresponding new number, but the fact that some of the numbers in the filenames repeat have me a little stuck since I'm just trying to learn regex.

The output I want would look like this (keeping the same order as above):

aaaa_bbb_01_cccc_256_a
aaaa_bbb_01_cccc_256_b
aaaa_bbb_01_cccc_256_c
aaaa_bbb_01_cccc_002_a
aaaa_bbb_01_cccc_078_a 

How can I change these filenames using regex? If it's not possible using regex what other alternative could I use?

Answer

Extract the model (rules); let it be a dictionary:

private static readonly Dictionary<string, string> s_Substitutes = new () {
  { "000", "256" },
  { "007", "002" },
  { "056", "078" },
};

If you insist on operating with tab separated text, you can try Split and Linq to build the dictionary:

string rules = @"000    256
                 007    002
                 056    078";

s_Substitutes = rules
  .Split(new char[] {'\r', '\n'})
  .Where(line => !string.IsNullOrWhiteSpace(line))
  .Select(line => line.Split('\t'))
  .ToDictionary(items => items[0].Trim(), items => items[1].Trim());

Then Regex.Replace will do the task:

string text = "aaaa_bbb_01_cccc_000_a";

...

string result = Regex.Replace(
   text, 
  "[0-9]{3}", 
   match => s_Substitutes.TryGetValue(match.Value, out var newValue) 
     ? newValue 
     : match.Value);

Comments