I'm working on a application that reads and writes to file based on config file created by user. As part of it, I want to output read text in provided by user pattern, and I can't figure out best way to do this.
Example:
Output patterns in config:
{Name} version: {1}{Name}_{1}
while:Name - will needs to be replaced with String variable value
1,2,3... - needs to be replaced with corresponding Regex Match Group value.
Regex.Replace
doesn't really work for me. It would go well with attributes like {Name}
, but I cannot convert a number to regex match group.
My second idea was to match pattern against {}
values and replace them using switch case, but it doesn't look like the best idea:
Regex a = new Regex("{(.*)}");foreach (Match m in a.Matches(Pattern)){ switch (m.Groups[1].Value) { case "1": { return MatchedGroups[1].Value; } case "2": { return MatchedGroups[2].Value; } case "Name": { return Name; } }}
EDIT 1:
Example to ilustrate it better, what I have and what I want to obtain:
I have:
String InputText = "12/03/2015 *** [RandomText] // 1.04.1112V";String regex = @"([0-9/]*) \*\*\* \[([A-Za-z]*)\] \/\/ (.*)"; // #group 1 = 12/03/2015 // #group 2 = RandomText // #group 3 = 1.04.1112VString Name = "GoogleChrome";String OutputPattern1 = "{1} - {Name} version {3}";String OutputPattern2 = "{Name}_{3}";
And having above variables and pattern I want to output:
#1 : 12/03/2015 - GoogleChrome version 1.04.1112V
#2 : GoogleChrome_1.04.1112V
Output Patterns will be created by user, so I cannot predict them.