正则表达式解析XML文件,文件扩展名为只返回URL
我有一个转换成字符串 下面的XML文件我想解析XML文件只返回URL文件扩展正则表达式解析XML文件,文件扩展名为只返回URL
<?xml version="1.0" encoding="utf-8"?>
<channel>
<generator>GOA Celebrations</generator>
<generator>GOA Celebrations</generator>
<pubDate>13 Jan 2016</pubDate>
<title>GOA Celebrations</title>
<link>http://goframe.com.au/rss/herston-inbound</link>
<language>en</language>
<image>
<url>http://goafame.com.au/site/templates/img/logo.png</url>
</image>
<item>
<title>Herston Inbound - Wednesday, 13 Jan - Alex Peisker - 2015-12-21 09:26am - Live</title>
<description></description>
<pubDate>13 Jan 2016 10:32:01 AEST</pubDate>
<link>http://goafame.com.au/gallery/entry/2991/</link>
<enclosure url="http://goafame.com.au/site/assets/files/2991/final_cropped_shutterstock_114908098.jpg" length="" type="" />
<guid isPermaLink="false">c5c1cb0bebd56ae38817b251ad72bedb</guid>
</item>
<item>
<title>Dog 1</title>
<description></description>
<pubDate>13 Jan 2016 10:32:01 AEST</pubDate>
<link>http://goafame.com.au/gallery/entry/2991/</link>
<enclosure url="http://animaliaz-life.com/data_images/dog/dog4.jpg" length="" type="" />
</item>
<item>
<title>Dog 2</title>
<description></description>
<pubDate>13 Jan 2016 10:32:01 AEST</pubDate>
<link>http://goafame.com.au/gallery/entry/2991/</link>
<enclosure url="http://cdn1.theodysseyonline.com/files/2015/12/21/6358631429926013411708851658_Dog-Pictures.jpg" length="" type="" />
</item>
<item>
<title>Dog 3</title>
<description></description>
<pubDate>13 Jan 2016 10:32:01 AEST</pubDate>
<link>http://goafame.com.au/gallery/entry/2991/</link>
<enclosure url="https://i.ytimg.com/vi/AkcfB3z0_-0/maxresdefault.jpg" length="" type="" />
</item>
</channel>
我的问题是如何做到这一点使用
定期Exression
所需的输出是
http://goafame.com.au/site/templates/img/logo.png http://goafame.com.au/site/assets/files/2991/final_cropped_shutterstock_114908098.jpg http://animaliaz-life.com/data_images/dog/dog4.jpg http://cdn1.theodysseyonline.com/files/2015/12/21/6358631429926013411708851658_Dog-Pictures.jpg
这里就是我试图到目前为止
Regex linkParser = new Regex(@"\b(?:https?://|www\.)\S+\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
string rawString = doc.ToString();
int posCounter = 0;
foreach (Match m in linkParser.Matches(rawString))
{
posCounter++;
links.Add(new LinkModel
{
IsSelected = false,
XmlLink = m.Value,
NodePosition = posCounter
});
}
注: XML文件可以来自任何来源,其他一些URL不在链接元素中。有些甚至是嵌套的。这就是为什么我想使用RegEx而不是XDocument。
我的图案可以匹配您的样本。这里http://regexstorm.net/tester
https?://[^\s<"]+/[^\s<"]+(?:\.\w{3,4})
的想法进行测试,寻找跟一个文件名模式(一端与3,4个字符扩展名)具有防溅字符(/)的所有链接。
var allLinkValues = XDocument.Parse(doc.ToString())
.Root
.Elements("item")
.Select(itemElement => itemElement.Element("link").Value)
.ToList();
这里
XDocument.Parse(doc.ToString())
加载文件。 Root
指向根元素 然后我们选择所有“item”元素并选择“link”元素的值。
Linq2Xml的力量!
XPath,XmlDocument是您的其他选项。
一般情况下,如果字符串计划良好,(XML,JSON,RDF等)不会选择RegEx作为第一个选项。这些类型的文档有很好定义的解析器。
而上面的查询应该让你开始在Xml导航。
谢谢你的回答,但我这里的问题是xml是动态的,它可能来自任何rss提要,并且一些链接不在链接元素 –
好。那么正则表达式是有道理的。 –
为什么你需要正则表达式?使用Xpath,它将通过一行代码:) –
不要对此使用RegEx。只需使用一个XML解析器。例如XDocument。 –
恕我直言,使用正则表达式解析XML不是一个好主意。我会去DOM或SAX。 –