Skip to main content

SubDocument.FindAll(Regex) Method

Finds all occurrences of a character pattern specified by the regular expression.

Namespace: DevExpress.XtraRichEdit.API.Native

Assembly: DevExpress.RichEdit.v24.1.Core.dll

NuGet Packages: DevExpress.RichEdit.Core, DevExpress.Win.Navigation

Declaration

DocumentRange[] FindAll(
    Regex regex
)

Parameters

Name Type Description
regex Regex

A Regex object representing a regular expression to search.

Returns

Type Description
DocumentRange[]

An array of DocumentRange objects representing ranges in the document matching the specified pattern.

Exceptions

Type Description
RegexMatchTimeoutException

Thrown if the Regex object cannot find any matched string during the time-out interval specified by the Regex constructor. To resolve this exception, enlarge the time-out interval passed to the the Regex constructor or use the Regex constructor without the matchTimeout parameter.

Remarks

The default maximum length of a string that can be obtained in a regular expression search is 128 characters. Use the DocumentSearchOptions.RegExResultMaxGuaranteedLength property to change the maximum string length.

The SubDocument.FindAll method overload performs a search in a specific document part (main body, text box, header, footer, comment, footnote, or endnote). Iterate all document parts and call the SubDocument.FindAll method for each SubDocument to search throughout all document parts. Refer to the following example for a code snippet on how to iterate all sub-documents: How to Iterate through all Sub-documents in a Document

Example

This code snippet illustrates how to find all six-letter words in a document.

Document document = wordProcessor1.Document;
document.LoadDocument("Grimm.docx", DevExpress.XtraRichEdit.DocumentFormat.OpenXml);
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();

The following code snippets (auto-collected from DevExpress Examples) contain references to the FindAll(Regex) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also