Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

TextSelection.ReplacePattern Method

Replaces matching text throughout an entire text document.

Namespace: EnvDTE
Assembly: EnvDTE (in envdte.dll)

bool ReplacePattern (
	[InAttribute] string Pattern,
	[InAttribute] string Replace,
	[OptionalAttribute] [InAttribute] int vsFindOptionsValue,
	[OptionalAttribute] [InAttribute] out TextRanges Tags
boolean ReplacePattern (
	/** @attribute InAttribute() */ String Pattern, 
	/** @attribute InAttribute() */ String Replace, 
	/** @attribute InAttribute() */ /** @attribute OptionalAttribute() */ int vsFindOptionsValue, 
	/** @attribute InAttribute() */ /** @attribute OutAttribute() */ /** @attribute OptionalAttribute() */ /** @ref */ TextRanges Tags
JScript does not support passing value-type arguments by reference.



Required. The string to find.


Required. The text to replace each occurrence of Pattern.


Optional. A vsFindOptions constant indicating the behavior of ReplacePattern, such as how to search, where to begin the search, whether to search forward or backward, and the case sensitivity.


Optional. A TextRanges collection. If the matched text pattern is a regular expression and contains tagged subexpressions, then Tags contains a collection of EditPoint objects, one for each tagged subexpression.

Return Value

A Boolean value.

ReplacePattern for the TextDocument object replaces text like ReplacePattern for the TextSelection object, but it operates on the whole text document rather than just the selected text.

The ReplacePattern method for Visual Studio is incompatible with earlier versions of the ReplacePattern method, because regular expressions now have a different syntax.

public void ReplacePatternExample(DTE dte)
    // Create a new text file and insert 10 lines of text.
    dte.ItemOperations.NewFile(@"General\Text File", "", 
    TextSelection txtSel = (TextSelection)dte.ActiveDocument.Selection;
    TextDocument txtDoc = (TextDocument)dte.ActiveDocument.Object("");
    EditPoint editPnt = txtDoc.StartPoint.CreateEditPoint();
    for (int i = 1; i <= 10; i++)
        editPnt.Insert("This is a test." + Environment.NewLine);

    if (MessageBox.Show("Replace 'test' with 'done deal'?", "", 
        MessageBoxButtons.YesNo) == DialogResult.Yes)
        TextRanges dummy = null;
        txtSel.ReplacePattern("test", "done deal", 
            (int)vsFindOptions.vsFindOptionsNone, ref dummy);

Community Additions

© 2015 Microsoft