Condividi tramite


TextSelection.ReplacePattern(String, String, Int32, TextRanges) Method

Definition

Replaces matching text throughout an entire text document. This API has been deprecated in VS 2022 and above. Please refer the breaking API changes in VS 2022 for guidance on how to migrate API usage via modern find and repalce APIs.

public:
 bool ReplacePattern(System::String ^ Pattern, System::String ^ Replace, int vsFindOptionsValue, [Runtime::InteropServices::Out] EnvDTE::TextRanges ^ % Tags);
[System.Runtime.InteropServices.DispId(31)]
public bool ReplacePattern (string Pattern, string Replace, int vsFindOptionsValue = 0, out EnvDTE.TextRanges Tags = default);
[System.Runtime.InteropServices.DispId(31)]
public bool ReplacePattern (string Pattern, string Replace, int vsFindOptionsValue, out EnvDTE.TextRanges Tags);
[<System.Runtime.InteropServices.DispId(31)>]
abstract member ReplacePattern : string * string * int * TextRanges -> bool
Public Function ReplacePattern (Pattern As String, Replace As String, Optional vsFindOptionsValue As Integer = 0, Optional ByRef Tags As TextRanges = Nothing) As Boolean
Public Function ReplacePattern (Pattern As String, Replace As String, vsFindOptionsValue As Integer, ByRef Tags As TextRanges) As Boolean

Parameters

Pattern
String

Required. The string to find.

Replace
String

Required. The text to replace each occurrence of Pattern.

vsFindOptionsValue
Int32

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

Tags
TextRanges

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.

Returns

A Boolean value.

Attributes

Examples

Sub ReplacePatternExample(dte As DTE)  

    ' Create a new text file and insert 10 lines of text.  
    dte.ItemOperations.NewFile()  
    Dim txtSel As TextSelection = _  
        CType(dte.ActiveDocument.Selection, TextSelection)  
    Dim txtDoc As TextDocument = _  
        CType(dte.ActiveDocument.Object(), TextDocument)  
    Dim editPnt As EditPoint = txtDoc.StartPoint.CreateEditPoint()  
    Dim i As Integer  
    For i = 1 To 10  
        editPnt.Insert("This is a test." & vbCrLf)  
    Next i  

    If MsgBox("Replace 'test' with 'done deal'?", vbYesNo) = _  
        MsgBoxResult.Yes Then  
        txtSel.SelectAll()  
        txtSel.ReplacePattern("test", "done deal")  
    End If  

End Sub  
public void ReplacePatternExample(DTE dte)  
{  
    // Create a new text file and insert 10 lines of text.  
    dte.ItemOperations.NewFile(@"General\Text File", "",   
        Constants.vsViewKindPrimary);  
    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.SelectAll();  
        txtSel.ReplacePattern("test", "done deal",   
            (int)vsFindOptions.vsFindOptionsNone, ref dummy);  
    }  
}  

Remarks

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 text selection.

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

Applies to