\" (in the content of xml file) with a matching \"<\" and \"&a开发者_如何学Gomp;gt;\"" />
开发者

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 "&lt;" 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&lt;jkh</abc><def>e&lt;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>",@"<([^>]*?<)",@"&lt;$1");
var final = Regex.Replace(first,@"(>[^<]*?)>",@"$1&gt;");

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, @"<([^>]*?<)", @"&lt;$1");
            next = Regex.Replace(next, @"(>[^<]*?)>", @"$1&gt;");
        } while(next != current);
        Console.WriteLine(current);
        Console.ReadKey();
    }
}


s/<(?=[^<>]*<)/&lt;/g
s/>(?<=\>[^<>]*)/&gt;/g

In C#,

new Regex("<(?=[^<>]*<)").Replace(your_xml_string, "&lt;");
new Regex(">(?<=\>[^<>]*)").Replace(your_xml_string, "&gt;");

Not tested. I don't have C# on my hand.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜