Back to Devexpress

SubDocument.FindAll(Regex) Method

officefileapi-devexpress-dot-xtrarichedit-dot-api-dot-native-dot-subdocument-dot-findall-x28-system-dot-text-dot-regularexpressions-dot-regex-x29.md

latest7.3 KB
Original Source

SubDocument.FindAll(Regex) Method

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

Namespace : DevExpress.XtraRichEdit.API.Native

Assembly : DevExpress.RichEdit.v25.2.Core.dll

NuGet Package : DevExpress.RichEdit.Core

Declaration

csharp
DocumentRange[] FindAll(
    Regex regex
)
vb
Function FindAll(
    regex As Regex
) As DocumentRange()

Parameters

NameTypeDescription
regexRegex

A Regex object representing a regular expression to search.

|

Returns

TypeDescription
DocumentRange[]

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

|

Exceptions

TypeDescription
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.

csharp
Document document = wordProcessor1.Document;
document.LoadDocument("Grimm.docx", DevExpress.XtraRichEdit.DocumentFormat.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();
vb
Document document = wordProcessor1.Document
document.LoadDocument("Grimm.docx", DevExpress.XtraRichEdit.DocumentFormat.Docx)
document.InsertSection(document.Range.Start)

' Specify a regular expression that will find all six letter words.
Dim expr As New System.Text.RegularExpressions.Regex("\b\w{6}\b")
Dim sixLetterWords As 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 r
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 s
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.

winforms-richedit-document-api/CS/RichEditAPISample/CodeExamples/SearchAndReplace.cs#L20

csharp
// Perform the search.
DocumentRange[] found = document.FindAll(expr);
foreach (DocumentRange r in found)

wpf-richedit-document-api/CS/DXRichEditControlAPISample/CodeExamples/SearchAndReplaceActions.cs#L20

csharp
// Perform the search.
DocumentRange[] found = document.FindAll(expr);
foreach (DocumentRange r in found)

winforms-richedit-document-api/VB/RichEditAPISample/CodeExamples/SearchAndReplace.vb#L17

vb
' Perform the search.
Dim found As DevExpress.XtraRichEdit.API.Native.DocumentRange() = document.FindAll(expr)
For Each r As DevExpress.XtraRichEdit.API.Native.DocumentRange In found

wpf-richedit-document-api/VB/DXRichEditControlAPISample/CodeExamples/SearchAndReplaceActions.vb#L15

vb
' Perform the search.
Dim found() As DocumentRange = document.FindAll(expr)
For Each r As DocumentRange In found

See Also

SubDocument Interface

SubDocument Members

DevExpress.XtraRichEdit.API.Native Namespace