ReplacePattern Method
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. ArchiveDisclaimer

EditPoint.ReplacePattern Method

Finds a pattern in the specified range of text and replaces it with the specified text.

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

bool ReplacePattern (
	[InAttribute] TextPoint Point,
	[InAttribute] string Pattern,
	[InAttribute] string Replace,
	[OptionalAttribute] [InAttribute] int vsFindOptionsValue,
	[OptionalAttribute] [InAttribute] out TextRanges Tags
boolean ReplacePattern (
	/** @attribute InAttribute() */ TextPoint Point, 
	/** @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 endpoint of the specified range of text. The edit point and Point are the boundaries for replacement.


Required. The string to find.


Required. The replacement string for Pattern.


Optional. A vsFindOptions constant indicating the type of search to perform.


Optional. If the matched pattern is a regular expression containing tagged sub-expressions, then Tags contains a collection of TextRange objects, one for each tagged subexpression.

Return Value

true if replacement occurs successfully; otherwise, false.

ReplacePattern finds text similarly to FindPattern, except that it only searches the text between edit point and the argument Point. To find and replace in the entire document, use ReplacePattern. The Tags collection returns only information for the last matched pattern.

Sub ReplacePatternExample()
   Dim objTextDoc As TextDocument
   Dim objMovePt As EditPoint
   Dim objEditPt As EditPoint, iCtr As Integer

   ' Create a new text file.
   DTE.ItemOperations.NewFile("General\Text File")

   ' Get a handle to the new document and create an EditPoint.
   objTextDoc = DTE.ActiveDocument.Object("TextDocument")
   objMovePt = objTextDoc.EndPoint.CreateEditPoint
   objEditPt = objTextDoc.StartPoint.CreateEditPoint

   ' Insert ten lines of text.
   For iCtr = 1 To 10
      objEditPt.Insert("This is a test." & Chr(13))
   Next iCtr
   ' Replace all occurrences of "test" with "thing."
    objEditPt.ReplacePattern(objMovePt, "test", "thing", vsFindOptions.vsFindOptionsFromStart)
End Sub
© 2016 Microsoft