这是我当前的代码:
var formula = "Scan: \"sample.test\" OR 'batch.id' AND if (results.tune)))";
if (formula.IndexOf("field(", StringComparison.OrdinalIgnoreCase) == -1)
{
formula = Regex.Replace(formula, "[a-zA-Z]\\w+\\.[a-zA-Z_]\\w*", "field(\"$0\")");
}
输出如下所示:
"Scan: \"field(\"sample.test\")\" OR 'field(\"batch.id\")' AND if (field(\"results.tune\"))))"
但是,我想跳过前两场比赛。因此,如果引用了一个术语,请不要替换它。预期的输出应如下所示:
"Scan: \"sample.test\" OR 'batch.id' AND if (field(\"results.tune\"))))"
我设法通过两次传递获得了预期的结果:
var formula = "Scan: \"sample.test\" OR 'batch.id' AND if (results.tune)))";
if (formula.IndexOf("field(", StringComparison.OrdinalIgnoreCase) == -1)
{
formula = Regex.Replace(formula, "[a-zA-Z]\\w+\\.[a-zA-Z_]\\w*", "field(\"$0\")");
formula = Regex.Replace(formula, "([\"|'])field\\(\"([a-zA-Z]\\w+\\.[a-zA-Z_]\\w*)\"\\)\\1", "$1$2$1");
}
但是,我相信应该可以一次性完成,可能使用正则表达式排除功能,但我不知道如何。