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;
}