SubDocument.ReplaceAll(Regex, String) 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
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. |
Returns
Type | Description |
---|---|
Int32 | An integer, representing the number of replacements made. |
Remarks
All strings in the document 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));
Related GitHub Examples
The following code snippets (auto-collected from DevExpress Examples) contain references to the ReplaceAll(Regex, String) 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.