i am trying to create a regx expression for fluentbit parser and not sure how to drop specific characters from a string
<testsuite name="Activity moved" tests="1" errors="0" failures="0" skipped="0" time="151.109" timestamp="2022-09-05T16:22:53.184000">
Above is the input which is i have as a string and i want to make multiple keys out of it.
expected output:
name: Activity moved
tests: 1
errors: 0
failures: 0
skipped: 0
timestamp: 2022-09-05T16:22:53.184000
How can i achieve this please?
CodePudding user response:
try this:
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\""}
CodePudding user response:
Of course you can write your own parser but may be it's more comfortable to use Nokogiri?
require 'nokogiri'
doc = Nokogiri::XML(File.open("your.file", &:read))
puts doc.at("testsuite").attributes.map { |name, value| "#{name}: #{value}" }