Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

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();