Thursday, December 20, 2007

Renaming xml attributes in VS2005 using Regular Expressions



In our product at work we have zillions of xml test files to feed nUnit. Refactoring becomes a problem when we want to rename an attribute in all those files.
In this case I wanted to rename the "Survey" element's 'id' 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:

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


The "Find What" says find "<Survey " followed by any characters followed by " id=".
The "\1" takes the value of all the characters skipped with the {.*}.

No comments: