使用正则表达式如何在 XML 标记之间进行多次匹配?

发布于 2024-10-05 22:54:15 字数 955 浏览 0 评论 0原文

首先,在你说什么之前,我必须这样做,因为 RSS 格式错误,但我无法纠正它。因此,当我尝试使用 RSS 和 XML 解析器时,它们失败了,而且我只有前端访问权限。然而,我非常接近,但我不明白为什么这不匹配。

提要(它是一个 1 行字符串): http://pastebin.com/5dJhXCvf

第一个例子:

<title>(.+)</title>

我认为这在我的测试中效果很好:

<title>&quot;cterrorism task force&quot; location:oregon - Google News</title>

但是问题是它匹配所有内容,然后作为一个匹配,例如:

<title>&quot;cterrorism task force&quot; location:oregon - Google News</title><title>&quot;cterrorism task force&quot; 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

前事休说 2024-10-12 22:54:15

尝试非贪婪版本(.+?)<\/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.

野生奥特曼 2024-10-12 22:54:15

您发布的 RSS 是格式良好的 XML,但不是有效的 RSS(根据 W3C 提要验证器)。由于它的格式良好,您最好的选择仍然是使用 XML 解析器,而不是使用正则表达式。事实上,大多数 RSS 解析器也应该没问题,因为 RSS 因存在验证问题而臭名昭著(部分原因是早期规范不佳),因此任何值得使用的 RSS 解析器都不应该遇到任何验证问题的麻烦。 W3C 验证器正在报告。

顺便说一句,这看起来像谷歌新闻提要。您可以通过将输出参数从“rss”更改为“atom”来获取有效的Atom。例如:

http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&topic=h&num=3&output=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:

http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&topic=h&num=3&output=atom

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.

哭泣的笑容 2024-10-12 22:54:15

尝试使用惰性量词:

<title>([^<]+?)</title>

Try a lazy quantifier:

<title>([^<]+?)</title>
纵山崖 2024-10-12 22:54:15

通过添加 U 标志来尝试不贪婪的表达式:

"/<title>(.+)</title>/U"

这告诉它匹配最小的匹配,而不是最大的可用匹配。

Try an ungreedy expression by adding the U flag:

"/<title>(.+)</title>/U"

This tells it to match on the smallest match rather than the largest match available.

遮云壑 2024-10-12 22:54:15

许多解析器可以处理与规范的轻微偏差。任何与出色的 libxml2 库的绑定都能够处理格式不良的 XML。许多语言都有绑定。例如,以下 Ruby 代码片段可以很好地解析它:

require 'nokogiri'

xml = open('rss.txt').read
doc = Nokogiri::XML.parse(xml)
doc.xpath('//title').each do |title|
  puts title.inner_text
end

结果:

"joint terrorism task force" location:oregon - Google News
"joint terrorism task force" location:oregon - Google News
Federal and FBI Joint Terrorism Task Force are still flawed - OregonLive.com
Striking a fair balance - OregonLive.com
Blame the terrorists, not the FBI - Portland Tribune
Why Oregon? Why not?: Terrorism can strike anywhere - The Register-Guard
INDIVIDUAL TRAVEL UNDER ATTACK - NewsWithViews.com
The other terrorism-and pondering Portland - BlueOregon
Fla. dance troupe causes scare at Lincoln Tunnel - Northwest Cable News

编辑:根据您的评论,我发现您正在使用 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:

require 'nokogiri'

xml = open('rss.txt').read
doc = Nokogiri::XML.parse(xml)
doc.xpath('//title').each do |title|
  puts title.inner_text
end

Result:

"joint terrorism task force" location:oregon - Google News
"joint terrorism task force" location:oregon - Google News
Federal and FBI Joint Terrorism Task Force are still flawed - OregonLive.com
Striking a fair balance - OregonLive.com
Blame the terrorists, not the FBI - Portland Tribune
Why Oregon? Why not?: Terrorism can strike anywhere - The Register-Guard
INDIVIDUAL TRAVEL UNDER ATTACK - NewsWithViews.com
The other terrorism-and pondering Portland - BlueOregon
Fla. dance troupe causes scare at Lincoln Tunnel - Northwest Cable News

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文