How can I replace < and > in the content of xml file using regex?
How can i replace a "<" and a ">" (in the content of xml file) with a matching "<"
and "&a开发者_如何学Gomp;gt;"
(with a pre known set of tags) using a regex?
example: <abc>fd<jkh</abc><def>e>e</def>
should result with:
<abc>fd<jkh</abc><def>e<e</def>
it must be done with a regex! (no xml load and such...)
I think the pattern
<([^>]*<)
will match a <
that encounters another <
before >
(therefore not part of a tag)
...and the pattern
(>[^<]*)>
will match a >
that follows another >
var first = Regex.Replace(@"<abc>fd<jkh</abc><def>e>e</def>",@"<([^>]*?<)",@"<$1");
var final = Regex.Replace(first,@"(>[^<]*?)>",@"$1>");
EDIT:
This does work, but you have to pass over it multiple times. I'm sure there's a purer method, but this does work.
class Program
{
static void Main(string[] args)
{
var next = @"<abc>dffs<<df</abc>";
string current;
do
{
current = next;
next = Regex.Replace(current, @"<([^>]*?<)", @"<$1");
next = Regex.Replace(next, @"(>[^<]*?)>", @"$1>");
} while(next != current);
Console.WriteLine(current);
Console.ReadKey();
}
}
s/<(?=[^<>]*<)/</g
s/>(?<=\>[^<>]*)/>/g
In C#,
new Regex("<(?=[^<>]*<)").Replace(your_xml_string, "<");
new Regex(">(?<=\>[^<>]*)").Replace(your_xml_string, ">");
Not tested. I don't have C# on my hand.
精彩评论