110,533
社区成员
发帖
与我相关
我的任务
分享
public static void Main()
{
Console.WriteLine(IsValidFormula(new string[] { "HH", "HK" }, "1+HH*1+1"));//True
Console.WriteLine(IsValidFormula(new string[] { "HH", "HK" }, "1+HH1*1+1"));//False
Console.ReadKey();
}
public static bool IsValidFormula(string[] symbols, string test)
{
//^(?!.+?(HK|HH|\d+){2,})(?!.+?([+\-*/^]|HK|HH|\d+)\2)(?!.+[+\-*/^]$)([+\-*/^]|HK|HH|\d+)+$
StringBuilder builder = new StringBuilder();
foreach (string symbol in symbols)
{
builder.Append(Regex.Escape(symbol) + "|");
}
builder.Append(@"\d+");
string pattern = string.Format(@"^(?!.+?(?:{0}){{2,}})(?!.+?([+\-*/^]|{0})\1)(?!.+[+\-*/^]$)([+\-*/^]|{0})+$", builder.ToString());
return Regex.IsMatch(test, pattern);
}