给定一个这种形式的任意字符串
" 和 x = 'o'reilly' 和 y = 'o'reilly' 和 z = 'abc' "
任何人都可以建议用 '' 代替 ' 嵌入在 o'reilly 部分中的字符的直接方法吗?(它当然可以是任何带有嵌入撇号的序列)。
我无法控制字符串的一般模式。它可以是任何包含'zzz'bbb'形式的部分,甚至可能是'aaa'bbb'ccc',其中需要替换内部撇号,而不是“外部”撇号。
我已经尝试过正则表达式,以及所有其他的。我很困惑,但我必须解决它!
TIA。
如果您愿意假设嵌入的撇号总是在两侧出现字母数字字符(而不是空格),那么您可以简单地执行以下操作:
(\w)'(\w)
然后替换为:
\1\2
这将具有删除字母数字之间的撇号的效果。看看这个 RegExr:http: //regexr.com?376k9
例子:
'hello there i am a representative from 'o'reilly' publishing and i am '2'3' years old'
变成:
'hello there i am a representative from 'oreilly' publishing and i am '23' years old'
好的,我可能会在这个上使用正则表达式......因为你真正需要的是找到前后字母不是空格的撇号......应该很容易。
否则,您可以只运行 string.indexof("'") ,检查前后的位置是否不是空格...然后 BOOM ...将其从水中吹出来...
string test = "aoeu'aou ";
string bad_test = "aoeu ' aoeu";
var index = test.IndexOf("'");
if (!test[index-1].Equals(' ') && !test[index+1].Equals(' '))
Console.Out.WriteLine("Boom at index "+ index);
else
Console.Out.WriteLine("seems we have some spaces");
index = bad_test.IndexOf("'");
if (! bad_test[index - 1].Equals(' ') && !bad_test[index + 1].Equals(' '))
Console.Out.WriteLine("Boom at index " + index);
else
Console.Out.WriteLine("seems we have some spaces");
这将输出:
索引 4 处的繁荣
似乎我们有一些空间
这是一个适合您的工作示例:
class Program
{
    static void Main(string[] args)
    {
        var input = "and x = 'o'reilly' and y = 'o'reilly' and z = 'abc' ";
        var output = input
            .Select((c, i) => new { Character = c, Index = i })
            .Where(o =>
            {
                if (o.Character != Convert.ToChar("'")) { return false; }
                var i = o.Index;
                var charBefore = (i == 0) ? false :
                    char.IsLetter(input[i - 1]);
                var charAfter = (i == input.Length - 1) ? false :
                    char.IsLetter(input[i + 1]);
                return charBefore && charAfter;
            })
            .ToList();
        foreach (var item in output)
        {
            input = input.Remove(item.Index, 1);
            input = input.Insert(item.Index, "\"");
        }
        Console.WriteLine(input);
        if (Debugger.IsAttached) { Console.ReadKey(); }
    }
}
这输出:
and x = 'o"reilly' and y = 'o"reilly' and z = 'abc'
基本上,它所做的只是获取一个匿名对象列表,为每个字符提供一个索引,过滤那些以'字母为前缀和后缀的对象,然后根据这些对象的结果通过索引修改输入。
你在寻找这样的东西吗?
    var splittedString = " and x = 'o'reilly' and y = 'o'reilly' and z = 'abc' ".Split(new char[] {' '},
                                                                                       StringSplitOptions
                                                                                           .RemoveEmptyEntries);
    List<string> listStrings = splittedString.Select(s => s.Trim('\'').Replace('\'', '"')).ToList();
    var finalResult = "";
    var prevMatchIsEqualToSign = false;
    foreach (var s in listStrings)
    {
        if (prevMatchIsEqualToSign)
        {
            finalResult += "'" + s + "' ";
        }
        else if (s == "=")
        {
            finalResult += "= ";
            prevMatchIsEqualToSign = true;
            continue;
        }
        else
        {
            finalResult += s +" ";
        }
        prevMatchIsEqualToSign = false;
    }
    Console.WriteLine(finalResult);