I need to clean up some xml another parser missed
The symbol I need to get rid of is & (except when it's correctly used in a escape sequence of course). So I wan开发者_C百科t to find all the "Mr. & Mrs. Jones" and replrace them with "Mr. &
Mrs". So I'm looing for all instances of " &", or "& ". And I need to replace them with &
Sure sounds like a job for RegEx; which I SUCK AT! But I envy it if that helps.
Any Thanksgiving Answeres out there for me?
Try this:
Regex.Replace(input, @"&(?!\w+;|#\d+;|#x[0-9a-fA-F]+;)", "&");
This will replace any orphan &
for correct escaped sequence.
Use HttpUtility.HtmlEncode(your string)
?
If you're doing plain old string operations, the simplest (albeit probably not the most efficient) way to accomplish this is to just use two replaces:
string xml = "Mr. & Mrs. Jones ";
xml = xml.Replace("&", " ");
xml = xml.Replace(" ", " ");
You could probably use a StringBuilder and build a quick loop that looks for all the "&" which would work pretty well as well...
Regex would work great too, but I always forget post conditions...
**Edit: Forgot about other escape sequences, so this solution is incomplete at best **
精彩评论