Reputation: 693
I have the following string in a file called mib.txt:
[name=1.3.6.1.2.1.1.5.0, value=myrouter.ad.local (OCTET STRING)]
name the following code:
f = File.read("/temp/mib.txt")
name = f.match(/1.3.6.1.2.1.1.5.0/)
puts "device name is #{name}"
It returns the 1.3.6.1.2.1.1.5.0
just like I asked it to, but what I really want is to find the string the contains 1.3.6.1.2.1.1.5.0
and parse out the value myrouter.
Upvotes: 0
Views: 326
Reputation: 575
You must extend your regex to catch the value inside a regex group.
s = File.read("/temp/mib.txt")
m = s.match /\[name=1.3.6.1.2.1.1.5.0, value=([\S]+) \(OCTET STRING\)/
puts "device name is #{m[1]}"
Upvotes: 1
Reputation: 89903
You always could use scan
:
>> name = f.scan(/1.3.6.1.2.1.1.5.0, value=(\w+)/).flatten.to_s
=> "myrouter"
If you want the ad.local
part as well, then instead do:
>> name = f.scan(/1.3.6.1.2.1.1.5.0, value=([\w\.]+)/).flatten.to_s
=> "myrouter.ad.local"
Upvotes: 1