【问题标题】:RegEx parse XML file and return only url with file extensionRegEx 解析 XML 文件并仅返回带有文件扩展名的 url
【发布时间】:2016-06-17 20:38:12
【问题描述】:

我有以下转换为字符串的 xml 文件 我想解析 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>

我的问题是如何使用

正则表达式

期望的输出是

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

https://i.ytimg.com/vi/AkcfB3z0_-0/maxresdefault.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 的原因。

【问题讨论】:

  • 为什么需要正则表达式?使用 Xpath 只需一行代码:)
  • 不要为此使用 RegEx。只需使用 XML 解析器。以 XDocument 为例。
  • 恕我直言,使用正则表达式解析 XML 不是一个好主意。我会选择 DOM 或 SAX。
  • 是否可以使用 XDocumet 获取 url(注意 XML 文件是动态的,有些 url 可能不是来自附件标签)?

标签: c# regex xml


【解决方案1】:

我的图案可以与您的样品相匹配。在这里测试http://regexstorm.net/tester

https?://[^\s<"]+/[^\s<"]+(?:\.\w{3,4})

这个想法是找到所有具有启动字符 (/) 后跟文件名模式(以 3,4 个字符扩展名结尾)的链接。

【讨论】:

    【解决方案2】:
    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 提要,并且某些链接不在链接元素中
    猜你喜欢
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 2015-09-24
    • 2018-12-09
    • 2019-02-03
    相关资源
    最近更新 更多