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
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