Friday 16 March 2018

Ruby (2.6.0) Regex multiple matches

I'd love to extract multiple matches from one block of text using regex in Ruby.



Here's an example;




https://rubular.com/r/l2Mi2xmahACmsV
enter image description here



The regex caught two matches in the example.




  • host: test01.com

  • service: 000001




The regex seems okay, so now I will apply with Ruby.



--
[5] pry(main)> t.match(/Service\|(?\d*)\|/)[:service]
=> "000001"

[6] pry(main)> t.match(/Hostname\|(?[^|]*|)/)[:host]
=> "test01.com"
--



It's running and it's totally expected with single match, but how about multiple matches?



[7] pry(main)> t.match(/Hostname\|(?[^|]*)\||Service\|(?\d*)\|/)
=> #


It seems that It does not work. (host:nil)




Any ideas to extract multiple matches in Ruby?

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...