Welcome to Sol 3 Sign in | Join | Help
CS Search | Live Search Search

Sol 3

Home of Barrows Software Solutions, LLC

Regex Programming and Usage Notes

I suck at using Regex so far.  As I get solutions to specialized problems I'll be posting them here.  There are a lot of great resources for most regex needs already on the net so you won't find those answers here.  :)


Extra Closing character...

I needed a way to handle an extra closing tag character in a bunch of malformed XML documents my client was getting.  For example:

<element> attribute1="" attribute2="" attribute3="">
    <element1 />
    <element2 />
</element>

If you notice, the opening element tag is ended pre-maturely.  Dave Wanta came to my rescue with this little gem:

  private static Regex xmlRegex = new Regex(
   @"<(?<tagname>\w+)>(?<attributes>[^<>]+?)>",
   RegexOptions.IgnoreCase
   | RegexOptions.Multiline
   | RegexOptions.IgnorePatternWhitespace
   | RegexOptions.Compiled
   );
 
  private static string KeithReplace( string path, Encoding encoding )
  {
   StreamReader sr = new StreamReader( path, encoding );
   string text = sr.ReadToEnd();
   sr.Close();
   text =  xmlRegex.Replace( text, "<$1 $2>" );
   return text;
  }

CS Build: 2.1.61129.2
1999
Listed on the CS Listings Powered By Community Server Themed by nb development