正则表达式 - 循环匹配

时间:2014-03-06 17:32:16

标签: c# regex sgml

我正在解析一个sgml文件。每个条目都是这样的:

<key n=1>xzsd:test
    <sk id=1 hi=1>test
        <tag1>.............</tag1>
        <tag2>.............</tag2>
         ................
    </sk>
    <sk id=2>test2
        <tag1>.............</tag1>
        <tag2>.............</tag2>
         ................
    </sk>
</key>

我想用一些HTML标记替换<key n=1>...</key>。我目前正在替换tag标签,但是只检索<key>标签内的所有内容可能会更有效。

我怎样才能编译这段代码:

entry = Regex.Replace(entry, "<key .*?>.*</key>", "<div class='key'>$2<div>");//Only interested in the second match.
Regex skReg = new Regex(@"<sk...>", RegexOptions.Compiled);
foreach (Match ItemMatch in ItemRegex.Matches(entry))
{
       //Do parsing of contents of each sk tag
}

sgml没有任何换行符

1 个答案:

答案 0 :(得分:2)

这就是工作:

            var entry = "<key n=1>xzsd:test <sk id=1 hi=1>test <tag1>.............</tag1> <tag2>.............</tag2> ................ </sk> <sk id=2>test2 <tag1>.............</tag1> <tag2>.............</tag2> ................ </sk> </key>";

        string pattern = "<key .*?>(.*)</key>";
        Match match = Regex.Match(entry, pattern);
        while (match.Success)
        {
            Console.WriteLine("Found: {0}",
                              match.Groups[1].Value); //find only what is in (.*)
            match = match.NextMatch();
        }