0

我想检查一个字符串是否包含以下格式

   [QUOTE]
    Test sentence
    [/QUOTE]

如果是这样,那么我会这样做。

            string description = dr["description"].ToString();
            description = description.Replace("[QUOTE]", "<blockquote>");
            description = description.Replace("[/QUOTE]", "</blockquote>");

还行吧。

但是这个怎么样?

[QUOTE=Axio;26]
Test sentence
[/QUOTE]

另外在这里我想添加块引用标签,以及想要在这些标签中显示这个文本

“最初由 Axio 发布。单击此处”

当您单击“单击此处”时,您将转到该特定帖子。所以这应该是一个超链接” 26 是帖子 ID

这个怎么做?

4

3 回答 3

1

您可以使用正则表达式匹配 [QUOTE] 中的任何内容,然后使用Split分号对其进行转换。像这样的东西:

        var regexPattern = @"\[QUOTE[=]{0,1}([\d\w;]*)\](.|\r|\n)*\[/QUOTE\]";
        var test1 = @"[QUOTE=Axio;26]
            Test sentence
            [/QUOTE]";
        var test2 = @"[QUOTE]
            Test sentence
            [/QUOTE]";

        var regex = new Regex(regexPattern);

        var match = regex.Match(test1);
        if (match.Success)
        {
            if (match.Groups.Count > 1) //matched [QUOTE=...]
                match.Groups[1].Value.Split(';').ToList().ForEach(s => Console.WriteLine(s));
            else //matched [QUOTE]..
                Console.WriteLine("Matched [QUOTE]");
        }
        else Console.WriteLine("No match"); 
        Console.Read();
于 2016-11-01T15:36:02.097 回答
0
//Get the description text
var description = "[QUOTE=Axio;26]Orginall posted by Axio. Click here[/QUOTE]";
//Get your id
var id = description.Substring(description.IndexOf(";") + 1, description.IndexOf("]") - (description.IndexOf(";") + 1));

//replace with anchor with id and <blockquotes/>
var editedstring = description
     .Remove(description.IndexOf("["), description.IndexOf("]") + 1)
     .Insert(0, "<blockquote><a href=\"#" + id + "\">")
     .Replace("[/QUOTE]", "</a></blockquote>");

结果:

 <blockquote><a href="#26">Orginall posted by Axio. Click here</a> </blockquote>
Orginall 由 Axio 发布。点击这里
于 2016-11-01T15:50:55.213 回答
0

有很多方法可以做到这一点,例如:

 string des = dr["description"].ToString().Replace("\n", "");
 string info[] = des.SubString(des.IndexOf('=') + 1, des.IndexOf(']')).Split(';');
 string name = info[0];
 string id = info[1]
 string sentence = des.SubString(des.IndexOf(']') + 1, des.LastIndexOf('['));

当你得到这个时,你就知道该怎么做了。我是手写的,你可能需要自己调整(子字符串位置不确定是否需要添加/子 1)。

于 2016-11-01T15:29:10.210 回答