Replacing CDATA sections using Vim editor

Here’s a quick regular expression for finding and replacing CDATA sections in an XML file, using Vim editor. ViM’s regex syntax is slightly different than Perl’s and I had to spend a little while to come up with this expression. Hope this helps somebody, some day.

%s/<!\[CDATA\_.\{-}\]\]>//c

Here’s the key items explained:

  • \_ Matches any single character or the end-of-line.
  • \{-} Matches zero or more of the preceding atom, as few times as possible.
  • /c Confirm before replace, you can as well use /g to do at one shot without any confirmation.

Tags: , , ,

Copyright (c) 2000-2011 Alex N J. All rights reserved.
All and any opinion expressed here are solely mine and do not represent the views of my employer or any other person or organization related to me. Entries (RSS) and Comments (RSS).