在整个文本文档中替换匹配的文本。
命名空间:  EnvDTE
程序集:  EnvDTE(在 EnvDTE.dll 中)
语法
声明
Function ReplacePattern ( _
    Pattern As String, _
    Replace As String, _
    vsFindOptionsValue As Integer, _
    <OutAttribute> ByRef Tags As TextRanges _
) As Boolean
bool ReplacePattern(
    string Pattern,
    string Replace,
    int vsFindOptionsValue,
    out TextRanges Tags
)
bool ReplacePattern(
    [InAttribute] String^ Pattern, 
    [InAttribute] String^ Replace, 
    [InAttribute] int vsFindOptionsValue, 
    [InAttribute] [OutAttribute] TextRanges^% Tags
)
abstract ReplacePattern : 
        Pattern:string * 
        Replace:string * 
        vsFindOptionsValue:int * 
        Tags:TextRanges byref -> bool 
function ReplacePattern(
    Pattern : String, 
    Replace : String, 
    vsFindOptionsValue : int, 
    Tags : TextRanges
) : boolean
参数
- Pattern
 类型:System.String
 必选。要查找的字符串。
- Replace
 类型:System.String
 必选。用来替换 Pattern 的每一匹配项的文本。
- vsFindOptionsValue
 类型:System.Int32
 可选。一个 vsFindOptions 常数,指示 ReplacePattern 的行为,例如,如何搜索、从何处开始搜索、是向前搜索还是向后搜索以及是否区分大小写等。
- Tags
 类型:EnvDTE.TextRanges%
 可选。TextRanges 集合。如果匹配的文本模式是正则表达式并且包含带标记的子表达式,则 Tags 包含一组 EditPoint 对象,每个带标记的子表达式对应一个对象。
返回值
类型:System.Boolean
一个布尔值。
备注
TextDocument 对象的 ReplacePattern 替换文本的方式类似于 TextSelection 对象的 ReplacePattern,但是前者针对整个文本文档操作而不是只针对选定文本操作。
由于正则表达式的语法现在与过去不同,Visual Studio 的 ReplacePattern 方法与早期版本的 ReplacePattern 方法不兼容。
示例
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);
    }
}
.NET Framework 安全性
- 对直接调用方的完全信任。此成员不能由部分信任的代码使用。有关更多信息,请参见通过部分受信任的代码使用库。