【发布时间】:2012-06-25 10:31:53
【问题描述】:
我正在使用 Open XML Office SDK 2.0 在 word 文档中搜索字符串并列出这些字符串。
MatchCollection Matches;
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(txtLocation.Text, true))
{
string docText = null;
using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
{
docText = sr.ReadToEnd();
}
Regex regex = new Regex(@"\(.*?\)");
Matches = regex.Matches(docText);
}
int i = 0;
while (i < Matches.Count)
{ Label lb = new Label();
lb.Text = Matches[i].ToString();
lb.Location = new System.Drawing.Point(24, (28 + i * 24));
this.panel1.Controls.Add(lb);
i++;
}
问题是有时它返回正确的字符串,例如:(HelloWorld),但有时它与标签完全不同,例如:
我该如何摆脱这些?
【问题讨论】:
标签: c# regex string search ms-word