Skip to main content

SubDocument.ReplaceAll(Regex, String, DocumentRange) Method

Replaces all occurrences of a character pattern defined by a regular expression with a specified replacement string.

Namespace: DevExpress.XtraRichEdit.API.Native

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

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

Declaration

int ReplaceAll(
    Regex regex,
    string replaceWith,
    DocumentRange range
)

Parameters

Name Type Description
regex Regex

A Regex object representing the regular expression to match.

replaceWith String

A string to replace with. May contain special symbols that represent the contents of capturing parentheses in the regular expression.

range DocumentRange

A DocumentRange specifying the search and replace scope of the document.

Returns

Type Description
Int32

An integer, representing the number of replacements made.

Remarks

All strings in the specified document range that match the specified regular expression are replaced. You can use the contents of capturing parentheses in the replacement text via $1, $2, $3, etc. You can even use the named Capture items - they are denoted by named groupings in the replacement string, as illustrated in the code sample below.

Example

The following code snippet illustrates how the SubDocument.ReplaceAll method with Regular Expressions can be used to convert all US formatted dates (month/day/year) in the document to two European formats: year-month-day and day.month.year.

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System.Text.RegularExpressions;
//...
Document document = wordProcessor.Document;
document.AppendText("12\\14\\2014" + Environment.NewLine);
string pattern = @"(?<mm>\d{2}).(?<dd>\d{2}).(?<yyyy>\d{4})";
string replacementString = @"${yyyy}-${mm}-${dd} or ${dd}.${mm}.${yyyy}";
System.Text.RegularExpressions.Regex myRegEx =
    new System.Text.RegularExpressions.Regex(pattern);
int count = document.ReplaceAll(myRegEx, replacementString);
System.Windows.Forms.MessageBox.Show(String.Format("We've done {0} replacement(s).", count));
See Also