我想根据我的需要解析以下RSS输出: http://forum.ragezone.com/external.php?type=RSS2&forumids=427
但是,我并不熟悉Regex(我认为在这种情况下需要它)。 我有以下代码将整个输出下载为字符串:
WebClient wc = new WebClient();
Stream st = wc.OpenRead(textBox1.Text);
using (StreamReader sr = new StreamReader(st))
{
Output = sr.ReadToEnd();
}
但是,我不确定如何解析它。基本上,每个“项目” - 这是一个线程,就像这样:
<item>
<title>
<![CDATA[ [Release] [Add-on] Choice Dialog Easy Selections ]]>
</title>
<link>
http://forum.ragezone.com/f427/add-choice-dialog-easy-selections-979948-new/
</link>
<pubDate>Tue, 07 Jan 2014 16:04:20 GMT</pubDate>
<description>
<![CDATA[
*Note:* This won't affect your current scripts or harm them, but you may add it to whatever you...
]]>
</description>
<content:encoded>
<![CDATA[
<div><b>Note:</b> This won't affect your current scripts or harm them, but you may add it to whatever you want.<br /> <b>Example of usage: </b><br /> <br /> Before:<br /> <div class="bbcode_container">
<div class="bbcode_description">PHP Code:</div>
<hr /><code class="bbcode_code"><code><span style="color: #000000"> <span style="color: #0000BB">cm</span><span style="color: #007700">.</span><span style="color: #0000BB">sendSimple</span><span style="color: #007700">(</span><span style="color: #DD0000">"Hey! \r\nPlease select an option.#b\r\n#L0#Option 1.#l\r\n#L1#Option 2.#l\r\n#L2#Option 3.#l"</span><span style="color: #007700">);
<br /></span><span style="color: #0000BB"></span> </span> </code></code><hr />
</div>After:<br /> <div class="bbcode_container">
<div class="bbcode_description">PHP Code:</div>
<hr /><code class="bbcode_code"><code><span style="color: #000000"> <span style="color: #0000BB">cm</span><span style="color: #007700">.</span><span style="color: #0000BB">sendSimple</span><span style="color: #007700">(</span><span style="color: #DD0000">"Hey! \r\nPlease select an option."</span><span style="color: #007700">, </span><span style="color: #DD0000">"Option 1"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Option 2"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Option 3"</span><span style="color: #007700">);
<br /></span><span style="color: #0000BB"></span> </span> </code></code><hr />
</div><b>How to add:</b><br /> <br /> Navigate to NPCConversationManager, replace your current sendSimple method with the following:<br /> <br /> <div class="bbcode_container">
<div class="bbcode_description">PHP Code:</div>
<hr /><code class="bbcode_code"><code><span style="color: #000000"> <span style="color: #0000BB"></span><span style="color: #007700">public </span><span style="color: #0000BB">void sendSimple</span><span style="color: #007700">(</span><span style="color: #0000BB">String text</span><span style="color: #007700">, </span><span style="color: #0000BB">String</span><span style="color: #007700">... </span><span style="color: #0000BB">selections</span><span style="color: #007700">) {<br /> if (</span><span style="color: #0000BB">selections</span><span style="color: #007700">.</span><span style="color: #0000BB">length </span><span style="color: #007700">> </span><span style="color: #0000BB">0</span><span style="color: #007700">) </span><span style="color: #FF8000">// Adding this even if selections length is 0 will do anything, but whatever.<br /> </span><span style="color: #0000BB">text </span><span style="color: #007700">+= </span><span style="color: #DD0000">"#b\r\n"</span><span style="color: #007700">;<br /><br /> for (</span><span style="color: #0000BB">int i</span><span style="color: #007700">=</span><span style="color: #0000BB">0</span><span style="color: #007700">; </span><span style="color: #0000BB">i</span><span style="color: #007700"><</span><span style="color: #0000BB">selections</span><span style="color: #007700">.</span><span style="color: #0000BB">length</span><span style="color: #007700">; </span><span style="color: #0000BB">i</span><span style="color: #007700">++) {<br /> </span><span style="color: #0000BB">text </span><span style="color: #007700">+= </span><span style="color: #DD0000">"#L" </span><span style="color: #007700">+ </span><span style="color: #0000BB">i </span><span style="color: #007700">+ </span><span style="color: #DD0000">"#" </span><span style="color: #007700">+ </span><span style="color: #0000BB">selections</span><span style="color: #007700">[</span><span style="color: #0000BB">i</span><span style="color: #007700">] + </span><span style="color: #DD0000">"#l\r\n"</span><span style="color: #007700">;<br /> }<br /> </span><span style="color: #0000BB">sendSimple</span><span style="color: #007700">(</span><span style="color: #0000BB">text</span><span style="color: #007700">, </span><span style="color: #0000BB">id</span><span style="color: #007700">);<br /> }
<br /></span><span style="color: #0000BB"></span> </span> </code></code><hr />
</div></div>
]]>
</content:encoded>
<category domain="http://forum.ragezone.com/f427/">Releases</category>
<dc:creator>Fraysa</dc:creator>
<guid isPermaLink="true">
http://forum.ragezone.com/f427/add-choice-dialog-easy-selections-979948/
</guid>
</item>
我希望能够检索每个项目的标题,链接,内容和创建者。 例如,我发布的项目:它的标题是“[Release] [Add-on] Choice Dialog Easy Selections”,内容如下:
<content:encoded>
创作者是:
<dc:creator>Fraysa</dc:creator>
我该怎么做?我在搞乱Regex方面遇到了真正的问题。我试图分开它,但搞砸了。有人可以帮忙吗? 最好的问候
答案 0 :(得分:0)
您可以使用以下代码:
String lines="<title><![CDATA[ [Release] [Add-on] Choice Dialog Easy Selections ]]></title>";
String REGEX="(?<=<title><\\!\\[CDATA\\[)(.*)(?=\\]\\]></title>)";
Pattern pattern=Pattern.compile(REGEX);
Matcher matcher =pattern.matcher(lines);
if(matcher.find()){
System.out.println("Title : "+matcher.group());
}
我使用了lookbehind (?<=)
并向前看(?=)
个表达式。