officefileapi-120431-word-processing-document-api-examples-search-and-replace-how-to-find-words-with-specific-number-of-characters.md
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();
Private Sub SurroundingSub()
Dim document As Document = wordProcessor.Document
document.LoadDocument("Grimm.docx")
document.InsertSection(document.Range.Start)
'Specify a regular expression that will find all six letter words.
Dim expr As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("\b\w{6}\b")
Dim sixLetterWords As System.Collections.Specialized.StringCollection = New System.Collections.Specialized.StringCollection()
'Perform the search.
Dim found As DocumentRange() = document.FindAll(expr)
For Each r As DocumentRange In found
sixLetterWords.Add(document.GetText(r))
Next
document.BeginUpdate()
'Insert an ordered list of non-repetitive words in the beginning of the document.
Dim distinctWords = sixLetterWords.Cast(Of String)().Distinct().OrderByDescending(Function(s) s)
For Each s In distinctWords
document.InsertText(document.Range.Start, s.ToString() + Environment.NewLine)
Next
document.EndUpdate()
End Sub