With regex how do i match between an XML tag multiple times?

為{幸葍}努か 提交于 2019-11-29 17:01:11

Try non-greedy version <title>(.+?)<\/title>. Here you can test these things online.

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.

Try a lazy quantifier:

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

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!