我在这里有一个与基于特定模式的字符串操作有关的问题。我正在尝试使用 C# 将特定模式替换为预定义模式
例如:
情景#1
Input: substringof('xxxx', [Property2])
Output: [Property2].Contains('xxxx')
这个字符串可以在 linq 的Where
子句中使用。
我的溶胶:
var key= myString.Substring(myString.Split(',')[0].Length + 1, myString.Length - myString.Split(',')[0].Length - 2);
var value = myString.Replace("," + key, "").Replace([Key from Dictionary], [Value from Dictionary]);
Expected string: key + '.' + value.Replace("('", "(\"").Replace("')", "\")");
但这仅适用于上述情况。我想将它概括为以下场景的所有内容。
场景:
Input: [Property1] == 1234 and substringof('xxxx', [Property2]) and substringof('xxxx', [Property3])
Output: [Property1] == 1234 and [Property2].Contains('xxxx') and [Property3].Contains('xxxx')
Input: substringof('xxxx', [Property2]) and [Property1] == 1234 and substringof('xxxx', [Property3])
Output: [Property2].Contains('xxxx') and [Property1] == 1234 and [Property3].Contains('xxxx')
任何帮助,将不胜感激。提前非常感谢!!
最终解决方案:
var replaceRegex = new Regex("substringof\\(\\s*'(?<text>[^']*)'\\s*,\\s*(?<pname>[\\w\\[\\]]+)\\s*\\)");
input = replaceRegex.Replace(input, "${pname}.Contains(\"${text}\")");