[英]Ruby regx for xml attributes
我正在嘗試為 fluentbit 解析器創建一個 regx 表達式,但不確定如何從字符串中刪除特定字符
<testsuite name="Activity moved" tests="1" errors="0" failures="0" skipped="0" time="151.109" timestamp="2022-09-05T16:22:53.184000">
以上是我作為字符串的輸入,我想從中制作多個鍵。
預期 output:
name: Activity moved
tests: 1
errors: 0
failures: 0
skipped: 0
timestamp: 2022-09-05T16:22:53.184000
請問我怎樣才能做到這一點?
當然,您可以編寫自己的解析器,但使用 Nokogiri 可能更舒服?
require 'nokogiri'
doc = Nokogiri::XML(File.open("your.file", &:read))
puts doc.at("testsuite").attributes.map { |name, value| "#{name}: #{value}" }
嘗試這個:
str = "<testsuite name=\"Activity moved\" tests=\"1\" errors=\"0\" failures=\"0\" skipped=\"0\" time=\"151.109\" timestamp=\"2022-09-05T16:22:53.184000\">"
regexp = /\w*=".*?"/ # there's your regexp
str.scan(regexp).inject({}) do |h,m|
k,v=m.split("=")
h[k]=v
h
} # and this is how you make the requested hash
# => {"name"=>"\"Activity moved\"", "tests"=>"\"1\"", "errors"=>"\"0\"", "failures"=>"\"0\"", "skipped"=>"\"0\"", "time"=>"\"151.109\"", "timestamp"=>"\"2022-09-05T16:22:53.184000\""}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.