使用正则表达式删除标签之间的引号

Remove quotation marks between tags using regex

我一直在努力尝试删除 Rails 项目 Ruby 中特定标签内的 XML 文件中的所有引号。简单的问题是:如果且仅当它们在 XML 文件(使用 gsub)的描述标签内时,我如何删除所有现有的 "

例子

<xml attribute="stuff"><name>Two inch thing (2")</name><description>This thing is really "awesome"></description></xml>

这样就变成了

<xml attribute="stuff"><name>Two inch thing (2")</name><description>This thing is really awesome></description></xml>

我已经用正则表达式苦苦挣扎了几个小时,却一无所获。

myxml_file.gsub(<regex matching quotation marks>, "")

这是一个更大问题的一部分,我使用 gem "Ox" 解析 XML 文件,使用 Ox.load(myxml_file, mode: :hash) 加载 XML -file 但描述部分包含 Ox 似乎忽略的 CDATA(只是将其全部设置为 nil)所以我执行了一个 gsub 以删除 CDATA 标签,但随后一些描述似乎包含使 Ox 加载崩溃的引号。因此,这个问题可以(最好)在 Ox.load 部分中解决,例如告诉它忽略 CDATA 标签...

根据要求编辑:

我从 url 中获取 XML 文件(这是一个产品提要),在本例中是 gzip 压缩的(我很确定这不会影响问题,以防万一):

tmp_data = Net::HTTP.get(URI.parse(url))
gz = Zlib::GzipReader.new(StringIO.new(tmp_data))
data = gz.read 
@feed = Ox.load(data, mode: :hash)

这种情况下的产品描述看起来像这个例子(为了解决这个问题,我在其中添加了一个“):

<products><product><merchant_deep_link>https://www.sportlala.se/lopning-40y-edition-2-pack-thundercrus/22361/express</merchant_deep_link><display_price>SEK319</display_price><merchant_product_id>05353-392410-XS</merchant_product_id><merchant_image_url>https://www.sportlala.se/images/products/22361/1905353_392410_40y_Edition_2-Pack_Set_F.png</merchant_image_url><merchant_category></merchant_category><search_price>319</search_price><merchant_name>Sportlala SE</merchant_name><category_id>0</category_id><aw_deep_link>...</aw_deep_link><category_name></category_name><last_updated></last_updated><product_name>40y Edition 2-Pack Thunder/Crus</product_name><aw_product_id>24553291137</aw_product_id><aw_image_url>https://images2.productserve.com/?w=200&amp;h=200&amp;bg=white&amp;trim=5&amp;t=letterbox&amp;url=ssl%3Awww.sportlala.se%2Fimages%2Fproducts%2F22361%2F1905353_392410_40y_Edition_2-Pack_Set_F.png&amp;feedId=35735&amp;k=477d0110b807fbbbcddc9fb74c52fc30c401ca4a</aw_image_url><delivery_cost></delivery_cost><data_feed_id>35735</data_feed_id><description><![CDATA[I detta paket f&aring;r du tv&aring; av Craft&#39;s absolut b&auml;sta baslager jerseys. Dessa "jerseys" har samlat det b&auml;sta fr&aring;n Craft&#39;s kollektioner och har den absolut h&ouml;gsta kvalit&eacute;n! &nbsp; Material: 100% Polyester]]></description><merchant_id>17150</merchant_id><currency>SEK</currency><store_price></store_price><language></language></product></products>

这将使来自 Ox 的结果哈希中的 description=nil 我很确定这是由于标签中的 CDATA 包装(因为它始终为零,无论是否有引号(“ ) 与否。

我做了一个 gsub,用 gsub 删除了 CDATA(我现在删除了它,但它类似于 .gsub("<description><![CDATA[", "<description>").gsub("]]</description>", "</description>"),它有效地删除了 CDATA,但随后引出了引号问题。

因此,这个问题可以在(最好)"Ox load" 级别上通过一些尚未见过的配置或通过扩展到整个文本的 "-标记上的正则表达式来解决。

代码:

s = '<xml attribute="stuff"><name>Two inch thing (2")</name><description>This thing is really "awesome"></description></xml>'
t = s.gsub(/(<description>)(.*?)(<\/description>)/) do
  open_tag, content, end_tag = , , 
  content = content.gsub(/"/, '')  
  [open_tag, content, end_tag].join
end
p s
p t

输出:

"<xml attribute=\"stuff\"><name>Two inch thing (2\")</name><description>This thing is really \"awesome\"></description></xml>"
"<xml attribute=\"stuff\"><name>Two inch thing (2\")</name><description>This thing is really awesome></description></xml>"

限制:这是非常具体的 XML 的确切格式。对 XML 的许多不改变其含义的有效更改将破坏此代码。仅限于外用;仅按指示使用。如果出现严重的副作用,请停止使用此正则表达式。