我需要帮助修改C#普通EX pression [英] I need help modifying a C# regular expression

查看:156
本文介绍了我需要帮助修改C#普通EX pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用正则表达式来检测帖子中论坛的标签,如[报价]文字[/报价],然后用HTML格式替换它们。我张贴的问题<一href=\"http://stackoverflow.com/questions/5109071/forum-tags-what-is-the-best-way-to-implement-them\">Forum标签。什么是实现它们的最好方法?了解嵌套的标签有问题。

I am using a regex to detect forum tags within posts, such as "[quote]text[/quote]" and then replace them with HTML formatting. I posted a question Forum tags. What is the best way to implement them? about a problem with nested tags.

我有一个想法,但我需要别人的帮助,帮我上扩大(因为我吸在正则表达式)。

I have an idea, but I need someone's help to help me expand on it (because I suck at regex).

我需要这个正则表达式修改

I need this regex modified

字符串正则表达式= @\\ [([^ =] +)= \\ X22'] *(\\ S *?)'\\ X22] * \\(。+?)\\ [/(\\ 1)\\];

现在这个相匹配的标签名称的开始和结束标记,与之相匹配的组,标签的内容,就像在这个论坛标记 [URL = WWW等号后面的值可选值。 google.com]点击我[/ URL]

Right now this matches an opening and closing tag, with match groups for the tag name, the tag content, and an optional value like the value after the equals in this forum tag [url=www.google.com]click me[/url].

我需要的是为前pression相匹配的开始标记或结束标记,并有包含标签名称(包括'/'结束标记)的匹配组。然后,我想通过他们迭代排序是这样的:

What I need is for the expression to match the opening tag OR the closing tag and have a match group containing the tag name (including the '/' for the closing tag). I then want to iterate through them sort of like this:

Dictionary<string, int> tagCollection = new Dictionary<string, int>();
inputString = Regex.Replace(inputString, @"expression I'm asking for here",
match =>
{
    string tag = match.Groups[0].Value;
    bool isOpeningTag = tag.StartsWith("/");
    tag = isOpeningTag ? tag : tag.Replace("/","");
    int tagCount = 0;
    if (tagCollection.ContainsKey(tag) && isOpeningTag)
    {
        tagCount = tagCollection[tag];
        tagCollection[tag] = tagCount + 1;
    }
    else if (tagCollection.ContainsKey(tag) && !isOpeningTag)
    {
        tagCount = tagCollection[tag];
        tagCollection[tag] = tagCount - 1;
    }
    else if (!tagCollection.ContainsKey(tag) && isOpeningTag)
        tagCollection.Add(tag, tagCount);

    string newTag = match.Value.Replace(tag, tag + tagCount.ToString());
    return newTag;
});

所以,现在,每个标签被附加了一些,我可以使用原来的正则表达式来进行标记功能,妥善处理嵌套的标签作为独立的标签。所以真的,都是我从你们需要的是我列在我指定的方式进行修改。正则表达式

So now, each tag is appended with a number and I could use the original regex to perform the tag functions and properly handle nested tags as separate tags. So really, all I need from you guys is the regex I listed to be modified in the manner I specified.

随时提供其他建议很好,但我会问实际的答案集中在正则表达式的修改,而不是这是否是去对这个问题的最好办法。

谢谢!

推荐答案

\\ [([^ =] +)= \\ X22'] *(\\ S *?)'\\ X22] * \\] | \\ [/(\\ 1)\\]

这篇关于我需要帮助修改C#普通EX pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆