Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

Regular Expression Find and Replace in Visual Studio 2003

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
sujeshva
Asked:
sujeshva
  • 2
  • 2
3 Solutions
 
Rob SiklosCommented:
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
 
sujeshvaAuthor Commented:
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
 
Rob SiklosCommented:
Ok - no problem.  Search for this instead:
([A-Za-z][A-Za-z0-9]*)\s*\.\s*Dispose\(\)

Open in new window

0
 
sujeshvaAuthor Commented:
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now