TextSelection.ReplacePattern(String, String, Int32, TextRanges) 方法

定义

在整个文本文档中替换匹配的文本。

[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)>]
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

参数

Pattern
String

必需。 要查找的字符串。

Replace
String

必需。 要替换每个 模式的匹配项的文本。

vsFindOptionsValue
Int32

可选。 一个 vsFindOptions 常数,用于指示的行为 ReplacePattern(String, String, Int32, TextRanges) ,例如,如何搜索、从何处开始搜索、是向前搜索还是向后搜索以及是否区分大小写。

Tags
TextRanges

可选。 一个 TextRanges 集合。 如果匹配的文本模式是正则表达式并且包含带标记的子表达式,则 标记 包含 EditPoint 对象的集合,每个标记的子表达式对应一个对象。

返回

Boolean

一个布尔值。

属性

示例

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);  
    }  
}  

注解

ReplacePattern 对于 TextDocument 对象,将替换 ReplacePattern 对象的文本 TextSelection (如),但它操作整个文本文档,而不只是选择文本。

ReplacePattern 方法 Visual Studio 与方法的早期版本不兼容 ReplacePattern ,因为正则表达式现在具有不同的语法。

适用于