I'm having an issue with Regex.Replace
in C# as it doesn't seem to be replacing all occurrences of the matched pattern.
我和Regex有个问题。在c#中替换,因为它似乎没有替换所有匹配模式的出现。
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.net上测试过了,它的工作原理和它应该一样。
EDIT:
Sorry, copied the wrong version of the function. It now shows the correct code, that behaves incorrectly for me.
编辑:对不起,复制错误版本的功能。它现在显示了正确的代码,对我来说它的行为不正确。
3 个解决方案
#1
1
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.
我看到的最大问题是,您可能不希望您的regex变得贪婪。
try replacing the .*
with .*?
试着用。*替换。*?
#2
1
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();
}
#3
1
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. Not sure what the cause of the problem is, but this solved it:
这解决了这个问题,它还处理嵌套的标签。不知道为什么,但是不断的重建仍然会导致错误。它可能是我们的VS2010损坏了,没有正确构建,或者是框架损坏了。不确定问题的原因是什么,但这解决了问题:
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;
}
#1
1
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.
我看到的最大问题是,您可能不希望您的regex变得贪婪。
try replacing the .*
with .*?
试着用。*替换。*?
#2
1
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();
}
#3
1
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. Not sure what the cause of the problem is, but this solved it:
这解决了这个问题,它还处理嵌套的标签。不知道为什么,但是不断的重建仍然会导致错误。它可能是我们的VS2010损坏了,没有正确构建,或者是框架损坏了。不确定问题的原因是什么,但这解决了问题:
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;
}