开发者

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

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>&quot;cterrorism task force&quot; location:oregon - Google News</title>

But the issue is that it matches everything then as one match for example:

<title>&quot;cterrorism task force&quot; location:oregon - Google News</title><title>&quot;cterrorism task force&quot; 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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜