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

后端 未结 5 1557
再見小時候
再見小時候 2020-12-21 11:41

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

相关标签:
5条回答
  • 2020-12-21 12:10

    Try a lazy quantifier:

    <title>([^<]+?)</title>
    
    0 讨论(0)
  • 2020-12-21 12:13

    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 讨论(0)
  • 2020-12-21 12:16

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

    0 讨论(0)
  • 2020-12-21 12:20

    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.

    0 讨论(0)
  • 2020-12-21 12:22

    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.

    0 讨论(0)
提交回复
热议问题