-1

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?

Zain
  • 49
  • 6

2 Answers2

0

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).to_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"}
Les Nightingill
  • 5,662
  • 1
  • 29
  • 32
-1

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}" }
mechnicov
  • 12,025
  • 4
  • 33
  • 56
  • Thanks for the answer but i am looking fir a regex expression instead, something like `^.* – Zain Sep 06 '22 at 09:57