【问题标题】:Extract and Transform提取和转换
【发布时间】:2017-01-13 01:18:06
【问题描述】:

如何完成在标签中提取文本并转换它们的任务?

例子:

out formatted

输入:

[txtItalic]This is italic[/txtItalic] [txtBold] Bold Text [/txtBold]

输出: 这是斜体 粗体字

我正在使用这段代码来提取标签之间的文本,但问题是它只需要第一个标签的文本

string ExtractString(string s, string tag)
{
    var startTag = "[" + tag + "]";
    int startIndex = s.IndexOf(startTag) + startTag.Length;
    int endIndex = s.IndexOf("[/" + tag + "]", startIndex);
    return s.Substring(startIndex, endIndex - startIndex);

}

我想完成什么,以及在 stackoverflow 编辑器中究竟发生了什么......

         richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Bold);

        richTextBox1.AppendText("Bold Text");

        richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Regular);

        richTextBox1.AppendText("Normal Text");

要加粗文本,请使用 **** 和斜体**

【问题讨论】:

  • 您可能会阅读 SO 帖子 stackoverflow.com/questions/7377344/how-to-write-a-parser-in-c“如何编写解析器”。我认为对文本解析和语法分析器进行一些研究会对您有所帮助。
  • 想要的输出是计划文本还是格式化文本?因为如果是后者,那是特定于 where 之后您将放置该文本的内容。

标签: c# regex winforms replace richtextbox


【解决方案1】:

这是我认为您需要的一种方法:

private void YourMethod()
{
    Process("[txtItalic]This is italic[/txtItalic], this is normal, [txtBold]Bold Text[/txtBold] and now [txtUnderline]Underlined Text[/txtUnderline]. The end.");
}

private void Process(string textWithTags)
{
    MatchCollection matches = Regex.Matches(textWithTags, @"\[(\w*)\](.*)\[\/\1\]");

    int unformattedStartPosition = 0;
    int unformattedEndPosition = 0;
    foreach (Match item in matches)
    {
        unformattedEndPosition = textWithTags.IndexOf(item.Value);

        // Add unformatted text.
        AddText(textWithTags.Substring(unformattedStartPosition, unformattedEndPosition - unformattedStartPosition), FontStyle.Regular);

        // Add formatted text.
        FontStyle style = GetStyle(item.Groups[1]);
        AddText(item.Groups[2].Value, style);

        unformattedStartPosition = unformattedEndPosition + item.Value.Length;
    }

    AddText(textWithTags.Substring(unformattedStartPosition), FontStyle.Regular);
}

private FontStyle GetStyle(Group group)
{
    switch (group.Value)
    {
        case "txtItalic":
            return FontStyle.Italic;
        case "txtBold":
            return FontStyle.Bold;
        case "txtUnderline":
            return FontStyle.Underline;
        default:
            return FontStyle.Regular;
    }
}

private void AddText(string text, FontStyle style)
{
    if (text.Length == 0)
        return;

    richTextBox.SelectionFont = new Font(richTextBox.SelectionFont, style);
    richTextBox.AppendText(text);
}

如果你使用标准的 RichTextBox,结果如下:

当然,这只是一个起点。如果您想组合格式或任何其他功能,则必须添加它。 ;)

【讨论】:

    【解决方案2】:

    这应该为您完成工作:

    string s = "[txtItalic]This is italic[/txtItalic] [txtBold] Bold Text [/txtBold]";
    //creating a list of tags
    List<string> tags = new List<string>();
    tags.Add("txtItalic");
    tags.Add("txtBold");
    //iterating through each of the tags
    foreach (string tag in tags)
    {
        var startTag = "[" + tag + "]";
        int startIndex = s.IndexOf(startTag) + startTag.Length;
        int endIndex = s.IndexOf("[/" + tag + "]", startIndex);
        string s1 = s.Substring(startIndex, endIndex - startIndex);
        Console.Write(s1);
    }
    

    输出:

    这是斜体粗体文本

    注意:这只会提取标签之间的文本。

    【讨论】:

    • 很酷,但是如何添加到格式中?例如: if (tags = txtBold) { bold(s1) }
    • @KawyllainyVi string 对象没有格式化,WinForms、WPF 和 ASP.NET 等 UI 框架有。您需要展示如何在 UI 上显示文本,以便我们告诉您。
    • 我希望这个例子能帮助你想象我打算做什么pastebin.com/XcJZ2RAw
    • @KawyllainyVi 检查this
    猜你喜欢
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多