2014-06-28 39 views
0

我的问题是类同这一https://stackoverflow.com/questions/18944437/replacing-text-between-b-and-b-with-bold-text-in-c-sharp如何在文本加粗串

我想< * STRONG>标签

之间的粗弦我得到的字符串与

string pattern = @"<strong>(.*?)</strong>"; 
var matches = Regex.Matches(paragraf2.Range.Text, pattern) 
        .Cast<Match>() 
        .Select(m => m.Groups[1].Value) 
        .ToList(); 

我猜我需要像

foreach(string a in matches) 
{  
} 

但我无法fi指出里面写什么。

+0

你尝试了什么?您的标记似乎表明您将生成的文本放入Word文档中。是对的吗? – KevinS

+0

@KevinS是的。我想要的是找到属于的文本(匹配给出了字符串列表),并替换为单词文档中的粗体文本。 – ruqo

+0

http://stackoverflow.com/questions/9840199/c-sharp-microsoft-office-interop-word – har07

回答

0

如har07在注释中规定,你可以使用它像

 string pattern = @"<strong>(.*?)</strong>"; 
     var matches = Regex.Matches(paragraf2.Range.Text, pattern) 
       .Cast<Match>() 
       .Select(m => m.Groups[1].Value) 
       .ToList(); 
     Word.Find findObject = Application.Selection.Find; 
     foreach(string a in matches){ 
      findObject.ClearFormatting(); 
      findObject.Text = a; 
      findObject.Replacement.ClearFormatting(); 
      findObject.Replacement.Font.Bold = 1; 
      object replaceAll = Word.WdReplace.wdReplaceAll; 
      findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceAll, ref missing, ref missing, ref missing, ref missing); 
      } 
1

相关的你的情况(如果正确描述)时,可以通过修改字符串中使用在[https://stackoverflow.com/questions/18944437/replacing-text-between-b-and-b-with-bold-text-in-c-sharp]指定的溶液:

richTextBox1.SelectedText = richTextBox1.SelectedText.Replace("<b>", "").Replace("</b>", ""); 

如下所示:

richTextBox1.SelectedText = richTextBox1.SelectedText.Replace("<strong>", String.Empty).Replace("</strong>", String.Empty); 

或者,相应的解决方案(修改后的原件)如下:

private void richTextBox1_TextChanged(object sender, EventArgs e) 
{ 
    string regexString = "(?<=<strong>)(.*?)(?=</strong>)"; 
    Match matches = (Regex.Match(richTextBox1.Text, regexString)); 

    if (matches.Success) 
    { 
     int index1 = richTextBox1.Find("<strong>"); 
     int index2 = richTextBox1.Find("</strong>"); 
     richTextBox1.Select(index1 + 3, ((index2) - (index1 + 3))); 

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

有关参考下面的代码片段RichTextBox中的字体属性的详细信息(来自微软在线文档http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionfont%28v=vs.100%29.aspx):

private void ToggleBold() 
{ 
    if (richTextBox1.SelectionFont != null) 
    { 
     System.Drawing.Font currentFont = richTextBox1.SelectionFont; 
     System.Drawing.FontStyle newFontStyle; 

     if (richTextBox1.SelectionFont.Bold == true) 
     { 
     newFontStyle = FontStyle.Regular; 
     } 
     else 
     { 
     newFontStyle = FontStyle.Bold; 
     } 

     richTextBox1.SelectionFont = new Font(
     currentFont.FontFamily, 
     currentFont.Size, 
     newFontStyle 
    ); 
    } 
} 

RGDS,

+0

我不想只是全部删除它们,我需要它们以粗体显示。 – ruqo

+0

可悲的是它不工作没有range.SelectionFont或range.Text.SelectionFont – ruqo