C# 简单正则表达式-用C替换论坛主题中的报价信息#
我想这应该很简单 我有这个字符串:C# 简单正则表达式-用C替换论坛主题中的报价信息#,c#,regex,quotes,C#,Regex,Quotes,我想这应该很简单 我有这个字符串: [quote=Joe Johnson|1]Hi![/quote] 应该用类似于 <div class="quote">Hi!<div><a href="users/details/1">JoeJohnson</a></div></div> 谁能给我指出正确的方向吗 感谢您的帮助 试试这个: string pattern = @"\[quote=(.*?)\|(\d+)\]([\s\S]
[quote=Joe Johnson|1]Hi![/quote]
应该用类似于
<div class="quote">Hi!<div><a href="users/details/1">JoeJohnson</a></div></div>
谁能给我指出正确的方向吗
感谢您的帮助 试试这个:
string pattern = @"\[quote=(.*?)\|(\d+)\]([\s\S]*?)\[/quote\]";
string replacement =
@"<div class=""quote"">$3<div><a href=""users/details/$2"">$1</a></div></div>";
Console.WriteLine(
Regex.Replace(input, pattern, replacement));
string模式=@“\[quote=(.*?)\(\d+)\]([\s\s]*?)\[/quote\]”;
字符串替换=
@"$3";
控制台写入线(
Regex.Replace(输入、模式、替换));
这应该是您在dot net中的正则表达式:
\[quote\=(?(.*)\\;(?(.*))\](?(.*))\[\/quote\]
string name = regexQuote.Match().Groups["name"];
string id = regexQuote.Match().Groups["id"];
//..
为什么你不说你也要处理嵌套标签 我几乎从未使用过regex,但这里有一件事:
static string ReplaceQuoteTags(string input)
{
const string closeTag = @"[/quote]";
const string pattern = @"\[quote=(.*?)\|(\d+?)\](.*?)\[/quote\]"; //or whatever you prefer
const string replacement = @"<div class=""quote"">{0}<div><a href=""users/details/{1}"">{2}</a></div></div>";
int searchStartIndex = 0;
int closeTagIndex = input.IndexOf(closeTag, StringComparison.OrdinalIgnoreCase);
while (closeTagIndex > -1)
{
Regex r = new Regex(pattern, RegexOptions.RightToLeft | RegexOptions.IgnoreCase);
bool found = false;
input = r.Replace(input,
x =>
{
found = true;
return string.Format(replacement, x.Groups[3], x.Groups[2], x.Groups[1]);
}
, 1, closeTagIndex + closeTag.Length);
if (!found)
{
searchStartIndex = closeTagIndex + closeTag.Length;
//in case there is a close tag without a proper corresond open tag.
}
closeTagIndex = input.IndexOf(closeTag, searchStartIndex, StringComparison.OrdinalIgnoreCase);
}
return input;
}
static string ReplaceQuoteTags(字符串输入)
{
常量字符串closeTag=@“[/quote]”;
常量字符串模式=@“\[quote=(.*?)\(\d+?)\](.*?\[/quote\]”;//或您喜欢的任何内容
常量字符串替换=@“{0}”;
int searchStartIndex=0;
int closeTagIndex=input.IndexOf(closeTag、StringComparison.OrdinalIgnoreCase);
while(closeTagIndex>-1)
{
正则表达式r=新正则表达式(模式,RegexOptions.rightoleft | RegexOptions.IgnoreCase);
bool-found=false;
输入=r.更换(输入,
x=>
{
发现=真;
返回string.Format(替换,x.Groups[3],x.Groups[2],x.Groups[1]);
}
,1,closeTagIndex+closeTag.Length);
如果(!找到)
{
searchStartIndex=closeTagIndex+closeTag.Length;
//如果有一个关闭标签没有正确的对应和打开标签。
}
closeTagIndex=input.IndexOf(closeTag、searchStartIndex、StringComparison.OrdinalIgnoreCase);
}
返回输入;
}
你那里有很多贪婪的*
-s。如果你问我的话,最好说得更具体些。我想了想,但我只是抄了原稿。。也许他真的想找到任何类似的东西来告诉用户这是非法的。你忘记了替换字符串中的@非常有效!接受了这一点,对我来说,这是最明显的解决办法。只有一个问题。如何处理外部的引语内部的引语
static string ReplaceQuoteTags(string input)
{
const string closeTag = @"[/quote]";
const string pattern = @"\[quote=(.*?)\|(\d+?)\](.*?)\[/quote\]"; //or whatever you prefer
const string replacement = @"<div class=""quote"">{0}<div><a href=""users/details/{1}"">{2}</a></div></div>";
int searchStartIndex = 0;
int closeTagIndex = input.IndexOf(closeTag, StringComparison.OrdinalIgnoreCase);
while (closeTagIndex > -1)
{
Regex r = new Regex(pattern, RegexOptions.RightToLeft | RegexOptions.IgnoreCase);
bool found = false;
input = r.Replace(input,
x =>
{
found = true;
return string.Format(replacement, x.Groups[3], x.Groups[2], x.Groups[1]);
}
, 1, closeTagIndex + closeTag.Length);
if (!found)
{
searchStartIndex = closeTagIndex + closeTag.Length;
//in case there is a close tag without a proper corresond open tag.
}
closeTagIndex = input.IndexOf(closeTag, searchStartIndex, StringComparison.OrdinalIgnoreCase);
}
return input;
}