简体   繁体   English

RegEx.Replace仅替换第一次出现,需要全部

[英]RegEx.Replace is only replacing first occurrence, need all

I'm having an issue with Regex.Replace in C# as it doesn't seem to be replacing all occurrences of the matched pattern. 我在C#中遇到Regex.Replace问题,因为它似乎无法取代所有出现的匹配模式。

private string ReplaceBBCode(string inStr)  
{  
    var outStr = Regex.Replace(inStr, @"\[(b|i|u)\](.*?)\[/\1\]", @"<$1>$2</$1>", RegexOptions.IgnoreCase | RegexOptions.Multiline);  
    outStr = Regex.Replace(outStr, "(\r|\n)+", "<br />"); 
    return outStr; 
}

The input string: 输入字符串:

[b]Saint Paul's Food Kitchen[/b]    [b]  [/b]Saint Paul's food kitchen opens weekly to provide food to those in need.

The result: 结果:

<b>Saint Paul's Food Kitchen</b>    [b]  [/b]Saint Paul's food kitchen opens weekly to provide food to those in need.

I've tested this in regexhero.net and it works exactly as it should there. 我已经在regexhero.netregexhero.net进行了测试,它的工作原理与应该的一样。

EDIT: 编辑:
Sorry, copied the wrong version of the function. 抱歉,复制了该函数的错误版本。 It now shows the correct code, that behaves incorrectly for me. 现在,它显示正确的代码,对我而言,行为不正确。

The output I'm getting is completely different from what you say you're getting, but 我得到的输出与您说的得到的完全不同,但是

The biggest problem I see, is that you probably don't want your regex to be greedy. 我看到的最大问题是,您可能不希望您的正则表达式过于贪婪。

try replacing the .* with .*? 尝试将.*替换为.*?

No need for Regex: 无需正则表达式:

private static string ReplaceBBCode(string inStr)  
{  
    return inStr.Replace("[b]", "<b>").Replace("[/b]", "</b>")
                .Replace("[i]", "<i>").Replace("[/i]", "</i>")
                .Replace("[u]", "<u>").Replace("[/u]", "</u>")
                .Replace("\r\n", "\n")
                .Replace("\n", "<br />"); 
}

I like this one better: 我更喜欢这个:

private static string ReplaceBBCode(string inStr)  
{
    StringBuilder outStr = new StringBuilder();
    bool addBR = false;
    for(int i=0; i<inStr.Length; i++){
        if (addBR){
            outStr.Append("<br />");
            addBR = false;
        }
        if (inStr[i] == '\r' || inStr[i] == '\n'){
            if (!addBR)
                addBR = true;
        }
        else {
            addBR = false;
            if (i+2 < inStr.Length && inStr[i] == '[' 
                && (inStr[i+1] == 'b' ||  inStr[i+1] == 'i' ||  inStr[i+1] == 'u')
                && inStr[i+2] == ']'){
                outStr.Append("<").Append(inStr[i+1]).Append(">");
                i+=2;
            }
            else if(i+3 < inStr.Length && inStr[i] == '[' && inStr[i+1] == '/'
                && (inStr[i+2] == 'b' ||  inStr[i+2] == 'i' ||  inStr[i+2] == 'u')
                && inStr[i+3] == ']'){
                outStr.Append("</").Append(inStr[i+2]).Append(">");
                i+=3;
            }
            else
                outStr.Append(inStr[i]);
        }
    }
    return outStr.ToString();
}

This solved the issue, it also handles nested tags. 这解决了问题,它也处理嵌套标签。 Not sure why, but rebuilding over and over it still was causing errors. 不知道为什么,但是一遍又一遍地重建仍然导致错误。 Its possible our VS2010 is corrupted and not building properly, or that the framework is corrupted. 我们的VS2010可能已损坏,无法正确构建,或者框架已损坏。 Not sure what the cause of the problem is, but this solved it: 不知道是什么原因造成的,但是这解决了它:

private string ReplaceBBCode(string inStr)
{
    var outStr = inStr;
    var bbre = new Regex(@"\[(b|i|u)\](.*?)\[/\1\]", RegexOptions.IgnoreCase | RegexOptions.Multiline);
    while( bbre.IsMatch(outStr))
        outStr = bbre.Replace(outStr, @"<$1>$2</$1>");
    outStr = Regex.Replace(outStr, "(\r|\n)+", "<br />");
    return outStr;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM