Thursday, February 24, 2011

Visual Studio Regular Expression Editing tips

VS provides regular expressions in replacing string which can be a lifesaver. Here's a few examples of operations to be performed on many files:

Removing an xml attribute:
What the RE means: Find the string "


regex:  to delete the attribute "cluster" from the "Survey" element:
Find What: \<Survey {.*} cluster=[^ ]*
Replace With: \<Survey \1

Rename an attribute
In this case I want to rename the "Survey" element's 'id' attribute to 'registrarId', but not the other elements' 'id' attribute. The element name and the attribute to change must be on the same line for this to work. A tiny sliver of regular expression in the "Find and Replace" dialog did the trick: The "Find What" says find "

Find What:  \<Survey {.*} id=
Replace with: \<Survey \1 registrarId=


No comments: