【问题标题】:Selecting Bold Text选择粗体文本
【发布时间】:2011-07-05 16:29:30
【问题描述】:

我只需要在 winform 应用程序的 RichTextBox 中选择粗体文本,然后将其括在括号内: 例如:The <strong>Rollup Action</strong> element describes the desired action that should be applied to the cluster activity that defines the <strong>Rollup Rule</strong>
粗体文本将变为:<br>[<strong>Rollup Action</strong>] [<strong>Rollup Rule</strong>]。谢谢。

【问题讨论】:

  • winforms,对不起,我没有提到。
  • 在结果文本中,您只需要带括号的粗体文本或所有文本,只需将括号添加到粗体?
  • 我需要在粗体部分添加括号的所有文本。

标签: c# winforms richtextbox richtext


【解决方案1】:

一种解决方案是使用正则表达式查找粗体文本并将其替换为相同的内容,但添加了括号:

  richTextBox.Rtf = Regex.Replace(richTextBox.Rtf, @"\\b ((\w| )*)", RegExSample.AddBrackets);

还有 MatchEvaluator:

public class RegExSample
{
      public static string AddBrackets(Match match)
      {
           return String.Format("[{0}]", match.Value);
      }
}

您的样本的输出将是:

[Rollup Action] 元素描述 应该采取的行动 应用于集群活动 定义[汇总规则]

您还可以更新正则表达式以确保它在所有情况下都能正常工作。

【讨论】:

  • 感谢阿德里安,它有效。但是,当粗体与另一种格式(例如斜体)结合使用时,它会继续到下一个匹配项。是否存在包含所有格式可能性的表达式?
猜你喜欢
  • 2013-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-09
  • 2014-09-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多