使用正则表达式如何在 XML 标记之间进行多次匹配?
首先,在你说什么之前,我必须这样做,因为 RSS 格式错误,但我无法纠正它。因此,当我尝试使用 RSS 和 XML 解析器时,它们失败了,而且我只有前端访问权限。然而,我非常接近,但我不明白为什么这不匹配。
提要(它是一个长 1 行字符串): http://pastebin.com/5dJhXCvf
第一个例子:
<title>(.+)</title>
我认为这在我的测试中效果很好:
<title>"cterrorism task force" location:oregon - Google News</title>
但是问题是它匹配所有内容,然后作为一个匹配,例如:
<title>"cterrorism task force" location:oregon - Google News</title><title>"cterrorism task force" location:oregon - Google News</title>
等于我的数组中的 1 个结果项,来自 exec()
和 match()
所以我尝试了:
<title>([\w\d\s\=\%\_\`\~\+\!\@\#\$\%\^\&\*\(\)\:\'\"\[\]\{\}\|\,\.\/]+)</title>
但是什么也不返回...有什么想法吗?
First, before you say anything, i HAVE to do this because the RSS is malformed, but i can't correct it on my end. So, while I tried using an RSS and a XML parser, they fail and i only have front end access. However, i'm super close, but i can't figure out why this wont match.
The feed (it's a long 1 line string):
http://pastebin.com/5dJhXCvf
First Example:
<title>(.+)</title>
This i thought worked great with my test of just:
<title>"cterrorism task force" location:oregon - Google News</title>
But the issue is that it matches everything then as one match for example:
<title>"cterrorism task force" location:oregon - Google News</title><title>"cterrorism task force" location:oregon - Google News</title>
Equals 1 result item in my array from exec()
and match()
So i tried:
<title>([\w\d\s\=\%\_\`\~\+\!\@\#\$\%\^\&\*\(\)\:\'\"\[\]\{\}\|\,\.\/]+)</title>
But that returns nothing... Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试非贪婪版本(.+?)<\/title></code>。 <a href="http://www.regular-expressions.info/javascriptexample.html" rel="noreferrer">在这里</a>您可以在线测试这些内容。
Try non-greedy version
<title>(.+?)<\/title>
. Here you can test these things online.您发布的 RSS 是格式良好的 XML,但不是有效的 RSS(根据 W3C 提要验证器)。由于它的格式良好,您最好的选择仍然是使用 XML 解析器,而不是使用正则表达式。事实上,大多数 RSS 解析器也应该没问题,因为 RSS 因存在验证问题而臭名昭著(部分原因是早期规范不佳),因此任何值得使用的 RSS 解析器都不应该遇到任何验证问题的麻烦。 W3C 验证器正在报告。
顺便说一句,这看起来像谷歌新闻提要。您可以通过将输出参数从“rss”更改为“atom”来获取有效的Atom。例如:
Google 生成提要的服务通常在生成 Atom 方面比 RSS 做得更好。也就是说,您可能还想向 Google 报告无效的 RSS。
The RSS you posted is well-formed XML, but not valid RSS (according to the W3C feed validator). Since it's well-formed your best bet is still to use an XML parser, not to use a regex. In fact, most RSS parsers should be ok too, as RSS is kind of notorious for having validation issues (partly due to poor specifications early on), so any RSS parser worth using shouldn't have any trouble with the kinds of validation problems the W3C validator is reporting.
As an aside, that looks like a Google News feed. You can get valid Atom by changing the output parameter from "rss" to "atom". eg:
Google's services that generate feeds generally do a better job at producing Atom rather than RSS. That said, you may also want to report the invalid RSS to Google.
尝试使用惰性量词:
Try a lazy quantifier:
通过添加 U 标志来尝试不贪婪的表达式:
这告诉它匹配最小的匹配,而不是最大的可用匹配。
Try an ungreedy expression by adding the U flag:
This tells it to match on the smallest match rather than the largest match available.
许多解析器可以处理与规范的轻微偏差。任何与出色的 libxml2 库的绑定都能够处理格式不良的 XML。许多语言都有绑定。例如,以下 Ruby 代码片段可以很好地解析它:
结果:
编辑:根据您的评论,我发现您正在使用 jQuery。您应该能够使用 jQuery XML 解析器来提取标题(以及其他部分,根据需要)。
Many parsers can handle slight deviations from the specs. Any binding to the excellent libxml2 library would be able to handle poorly formed XML. There are bindings in many languages. For example, the following Ruby snippet parses it just fine:
Result:
Edit: based on your comments I see you're using jQuery. You should be able to use a jQuery XML parser to extract the titles (and other parts, as needed).