Frage

I'm parsing a sgml file. Each entry is something like this:

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

I want to replace <key n=1>...</key> with some HTML markup. I'm currently replacing tag by tag, but it would probably be more efficient to just retrieve everything inside the <key> tag.

How could I make this code compile:

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
}

The sgml does not have any newlines

War es hilfreich?

Lösung

This does the job:

            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();
        }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top