0

我有一个正则表达式,想忽略字符串中的任何 ' (撇号)。正则表达式讨论可以在讨论字符串操作中找到:如何用特定模式替换字符串

RegEx: \\(\\s*'(?<text>[^'']*)'\\s*,\\s*(?<pname>[\\w\\[\\]]+)\\s*\\)

基本上,提供的 RegEx 在 {text} 包含 ' (撇号)的情况下不起作用。您能否让 RegEx 忽略 {text} 中的任何撇号?

For eg: 

substringof('B's',Name) should be replaced by Name.Contains("B's") 
substringof('B'',Name) should be replaced by Name.Contains("B'")
substringof('''',Name) should be replaced by Name.Contains("'")

欣赏它!谢谢你。

4

1 回答 1

1

这案子似乎很难处理''''。这就是我选择使用委托和另一个替换来解决问题的原因。

static void Main(string[] args)
{
    var subjects = new string[] {"substringof('xxxx',Name)", "substringof('B's',Name)", "substringof('B'',Name)", "substringof('''',Name)"};

    Regex reg = new Regex(@"substringof\('(.+?)'\s*,\s*([\w\[\]]+)\)");
    foreach (string subject in subjects) {
        string result = reg.Replace(subject, delegate(Match m) { return m.Groups[2].Value + ".Contains(\"" + m.Groups[1].Value.Replace("''", "'") + "\")"; });
        Console.WriteLine(result);
    }
}
于 2013-05-13T18:31:02.500 回答