How to: Find Words with Specific Number of Characters
This example demonstrates how to use regular expressions to find words with specific number of characters (6 in this example). Call the SubDocument.FindAll method to perform the search.
Document document = wordProcessor.Document;
document.LoadDocument("Grimm.docx");
document.InsertSection(document.Range.Start);
//Specify a regular expression that will find all six letter words.
System.Text.RegularExpressions.Regex expr = new System.Text.RegularExpressions.Regex("\\b\\w{6}\\b");
System.Collections.Specialized.StringCollection sixLetterWords = new System.Collections.Specialized.StringCollection();
//Perform the search.
DocumentRange[] found = document.FindAll(expr);
foreach (DocumentRange r in found)
{
sixLetterWords.Add(document.GetText(r));
}
document.BeginUpdate();
//Insert an ordered list of non-repetitive words in the beginning of the document.
var distinctWords = sixLetterWords.Cast<string>().Distinct().OrderByDescending(s => s);
foreach (var s in distinctWords)
{
document.InsertText(document.Range.Start, s.ToString() + Environment.NewLine);
}
document.EndUpdate();