Reputation: 759
I have a formula in french that I want to convert to english See below:
Initial: =SI(renamed="SI";SI(f1<f4;"SI(";"nok");"1;2")
Must be converted to: =IF(renamed="SI",IF(f1<f4,"SI(";"nok"),"1;2")
2 issues:
; must be converted to , ( but not inside strings " xxx ; xxx "
SI must be renamed to IF ( but not inside strings " xxx SI xxx "
I have this regex as a good beginning :([^=;]*)\(
at
https://regex101.com/r/OPyLh1/3
Actually, this is just a sample with the work IF /SI but I have around one hundred words to convert between french and english
What would be the best approach to achieve this ( a mix of regex + code ?) we are doing development in C#
Upvotes: 0
Views: 113
Reputation: 27723
Design of an expression for this problem is rather complicated, might depends on inputs that we might have, and understanding the technicalities, which I'm simply not sure about the last. Then, we can define a set of rules to find what we wish to replace. For example, this expression would capture those ;
and SI
included in the question using logical ORs:
(SI)|["](;)[A-Z]|[0-9](;)["]|[)](;)["]
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(SI)|[""](;)[A-Z]|[0-9](;)[""]|[)](;)[""]";
string input = @"=SI(renamed=""SI"";SI(f1<f4;""SI("";""nok"");""1;2"")";
RegexOptions options = RegexOptions.Multiline;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}
If this expression wasn't desired or you wish to modify it, please visit regex101.com.
jex.im visualizes regular expressions:
Upvotes: 1