【发布时间】:2013-05-04 01:52:18
【问题描述】:
我有一个关于基于特定模式的字符串操作的问题。我正在尝试使用 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}\")");
【问题讨论】:
-
你从哪里得到 substringof 方法?还是您正在尝试创建的方法?
-
这看起来像是一个正则表达式替换的工作。你试过了吗?
-
我正在从 Kendo 网格中获取 substringof,并将其传递到 LINQ 的 WHERE 子句中,我必须将其转换为包含。还没有尝试过正则表达式。
标签: c# string string-matching substring