Visual Studio - Regex Find and Replace for CSS .Class and #id

Recently I've been working on a project which was needed to be retroactively be included into varying 3rd party application which meant that all the CSS written needed to be prefixed uniquely and because of the sheer size of the CSS the only possible solution was through using Regex find and replace.

 

The VS Regex code

The built-in Visual Studio Regex is pretty comprehensive though doesn't really adhere to other regular expression syntax. Here is the code:

 

To find: {\#|\.}{:c*}{:b|\n|\{}

  • {\#|\.} - This finds the # or . which prefixs CSS ID's and Classes respectively the {} dictate it's something to be saved and replaced,
  • {:c*} - This finds alphabetic characters followed by anything untill the next expression match.
  • {:b|\n|\{} - This then finds either the affixed space (:b), line-break (\n) or opening curly bracket (\{)


To Replace: \1SOMEPREFIXHERE-\2\3

This is quite easy to understand, the \1 \2 \3 are representing the saved elements previously described in the find statement that are wrapped in { }. and the rest is statix text i.e. the prefix which needs to be injected into all the ID's and Classes's.

 

Screenshot of the replace

class id css regex visual studio 2010

 

Any tips or ways to make this more efficient then let me know!

ASP.NET

Buy me a beer or a book?

amazon PayPal - The safer, easier way to donate online!

Free Downloads