views:

29

answers:

1

I want create macro for replacing. But my problem is how to use regular expression in Visual Basic's macro for Visual Studio?

document.Selection.ReplacePattern("test{[^']+}test", "testAAAAtest")

Doesn't work.

A: 

First does the RegEx actually match something? Try it in the Find Dialog first.

Second you will need to tell the Replace Pattern how to match - It's essentially the same as doing a Find/Replace.

Here is something to get you started: (Note the vsFindOptions.vsFindOptionsRegularExpression)

Public Sub ReplaceRegEx()
    DTE.UndoContext.Open("RegEx Replace")
    Dim textSelection As TextSelection = DTE.ActiveDocument.Selection
    textSelection.ReplacePattern("test{[^']+}test", "testAAAAtest", vsFindOptions.vsFindOptionsRegularExpression)
    DTE.UndoContext.Close()
End Sub
Brian Schmitt