简单的正则表达式 - 替换 C# 中论坛主题中的引用信息

发布于 2024-08-08 05:09:55 字数 430 浏览 2 评论 0原文

我认为这应该是非常简单的。

我有这个字符串:

[quote=Joe Johnson|1]Hi![/quote]

应该用类似的东西替换,

<div class="quote">Hi!<div><a href="users/details/1">JoeJohnson</a></div></div>

我很确定这进展不太顺利。到目前为止,我有这个:

Regex regexQuote = new Regex(@"\[quote\=(.*?)\|(.*?)\](.*?)\[\/quote\]");

有人能指出我正确的方向吗?

任何帮助表示赞赏!

This should be pretty straightforward I would think.

I have this string:

[quote=Joe Johnson|1]Hi![/quote]

Which should be replaced with something like

<div class="quote">Hi!<div><a href="users/details/1">JoeJohnson</a></div></div>

I'm pretty shure this is not going very well. So far I have this:

Regex regexQuote = new Regex(@"\[quote\=(.*?)\|(.*?)\](.*?)\[\/quote\]");

Can anyone point me in the right direction?

Any help appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰魂雪魄 2024-08-15 05:09:55

试试这个:

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));

Try this:

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));
天赋异禀 2024-08-15 05:09:55

你为什么不说你也想处理嵌套标签...

我几乎没有使用过正则表达式,但事情是这样的:

    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;
    }

why didn't you said you want to deal with nested tags as well...

i've barely ever worked with regex, but here the thing:

    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;
    }
梦幻的心爱 2024-08-15 05:09:55

这应该是点网中的正则表达式:

\[quote\=(?(.*))\|(?(.*))\](?( .*))\[\/引用\]

        string name = regexQuote.Match().Groups["name"];
        string id = regexQuote.Match().Groups["id"];
        //..

this should be your regex in dot net:

\[quote\=(?<name>(.*))\|(?<id>(.*))\](?<content>(.*))\[\/quote\]

        string name = regexQuote.Match().Groups["name"];
        string id = regexQuote.Match().Groups["id"];
        //..
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文