Share via


TextSelection.ReplacePattern 方法

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

命名空间:  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
    类型:String

    必需。 要查找的字符串。

  • Replace
    类型:String

    必需。 用来替换 Pattern 的每一匹配项的文本。

  • vsFindOptionsValue
    类型:Int32

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

  • Tags
    类型:EnvDTE.TextRanges%

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

返回值

类型:Boolean
一个布尔值。

备注

TextDocument 对象的ReplacePattern 替换与 ReplacePattern 的文本 TextSelection 对象的,但是,它运行在文本文档而不是文本选择。

由于正则表达式的语法现在与过去不同,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 安全性

请参阅

参考

TextSelection 接口

EnvDTE 命名空间

其他资源

如何:编译和运行自动化对象模型代码示例