Solved

Regular Expression Find and Replace in Visual Studio 2003

Posted on 2008-06-13
4
189 Views
Last Modified: 2013-11-26
Hey Experts,

Quick and simple question.

I have multiple code files where the Finally block in each method has an object.Dispose().. like this

Finally
       x.Dispose()
       Y.Dispose()
End Try

x and y can vary in names based on what object is being disposed. I want to do a Find and Replace in the entire solution where I have to find a match for Any object.Dispose() and replace it with
If Not Object is Nothing Then
     Object.Dispose()
End If

i.e
The code will be something like

objSQL.Dispose or lobjSQL.Dispose() or objOracle.Dispose() and I have to replace them with

If not objectname Is Nothing Then
   objectname.Dispose()
End If

where objectname could be anything that I match with a single Regular expression and use that in the replace too..

Any ideas or thoughts or the expression itself is appreciated.

Thanks,
Suj
0
Comment
Question by:sujeshva
  • 2
  • 2
4 Comments
 
LVL 9

Assisted Solution

by:Rob Siklos
Rob Siklos earned 100 total points
ID: 21780284
I would use the Search/Replace features of EditPlus (www.editplus.com).  It lets you define complex regular expressions for search/replace in files.

So the regex would be something like this:

Search:    
    (objSQL | lobjSQL | objOracle)\.Dispose\(\)
Replace with:
    If not \1 Is Nothing Then
      \1.Dispose()
    End If
0
 
LVL 3

Author Comment

by:sujeshva
ID: 21780757
Thank you for the response, but the trouble with your response is that I need to have the names of the object handy, I have over 500 different variable names and it is not limited to objSQL or lobjSQL or objOracle. What I wanted was something like ([a-Z])\.Dispose\(\) being replaced with
If not \1 Is Nothing Then
\1.Dispose()
End If
0
 
LVL 9

Assisted Solution

by:Rob Siklos
Rob Siklos earned 100 total points
ID: 21780786
Ok - no problem.  Search for this instead:
([A-Za-z][A-Za-z0-9]*)\s*\.\s*Dispose\(\)

Open in new window

0
 
LVL 3

Accepted Solution

by:
sujeshva earned 0 total points
ID: 21781666
Found the answer!

Search Expression
{([a-zA-Z0-9]+)}\.Dispose\(\)

Replace Expression
If Not \1 Is Nothing Then \n \0 \n End If

Thanks,
Suj
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Best environment for cross platform web development? 5 55
SQL Help 27 57
SSIS On fail action 5 38
Change to file doesn't show up 16 57
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question