Friday, March 16, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...