# Searching Text for Multiple Matches

**URL:** <https://www.ruby-forum.com/t/searching-text-for-multiple-matches/179637>\
**Category:** Ruby\
**Created:** [November 30, 2009, 11:08pm UTC](https://www.ruby-forum.com/t/searching-text-for-multiple-matches/179637 "2009-11-30T23:08:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [November 30, 2009, 11:08pm UTC](https://www.ruby-forum.com/t/searching-text-for-multiple-matches/179637/1 "2009-11-30T23:08:13Z")

</div>

I am using ruby to search and match text in a file that has several  
mathes.

Here is the text:

Ethernet adapter Local Area Connection:

```
    Connection-specific DNS Suffix . : corp.pc.com
    IP Address. . . . . . . . . . . . : 172.1.81.7
    Subnet Mask . . . . . . . . . . . : 255.255.255.0
    Default Gateway . . . . . . . . . : 172.1.81.254

```

Ethernet adapter Network Connect Adapter:

```
    Media State . . . . . . . . . . . : Media disconnected

```

Ethernet adapter Wireless Network Connection:

```
    Connection-specific DNS Suffix . : corp.pc.com
    IP Address. . . . . . . . . . . . : 172.1.17.116
    Subnet Mask . . . . . . . . . . . : 255.255.255.0
    Default Gateway . . . . . . . . . : 172.1.17.254

```

What I need to do it pull out the IP address and default gateway IP  
addresses for each interface and store them for processing later on in  
the script.

What method should I use to get the first ip address and gateway and  
store it, then grab the next set and store that?

thanks

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [November 30, 2009, 11:27pm UTC](https://www.ruby-forum.com/t/searching-text-for-multiple-matches/179637/2 "2009-11-30T23:27:57Z")

</div>

> What method should I use to get the first ip address and gateway and  
> store it, then grab the next set and store that?

I’d probably recommend a regular expression.  
something like  
a = big\_string  
big\_string =~ /something/

or

big\_string.scan(/something/).each {|match|

}

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [November 30, 2009, 11:39pm UTC](https://www.ruby-forum.com/t/searching-text-for-multiple-matches/179637/3 "2009-11-30T23:39:40Z")

</div>

Thanks Roger. I am currently iterating through each line of text and  
matching on any lines that either have “IP Address” or “Default Gateway”  
in them and grabbing the IP addresses and assigning them to variables.

The problem is, the first IP address I get seems to disapear toward the  
end of the script and I can’t figure out why…perhaps because I’m  
using “elsif”?

here is the code:

ipconf = `ipconfig`

ipconf.each do |x|

```
 if
 x[/IP\sAdd*/]
 x.scan(/\s*IP\sAddress.*\:\s([1-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\s+$/)
 ip = $1
 puts "ip addr: " + $1.to_s ## THIS PRINTS OUT FINE

 elsif
 x[/Def*/]
 puts "XXXXX IP: #{ip}"
 x.scan(/\s*Default\sGateway.*\:\s([1-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\s+$/)
 def_gate = $1
 puts "Default Gateway: "+$1.to_s ## THIS PRINTS OUT FINE

 puts "Print IP address again: #{ip_addr}" ### THIS HAS NO VALUE

```

end  
end

I think I need to somehow combine the “if” and “elsif” statements so I’m  
not using “or” and this way the ip\_addr variable will hold it’s value?

thanks

jack

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [December 2, 2009, 11:31am UTC](https://www.ruby-forum.com/t/searching-text-for-multiple-matches/179637/4 "2009-12-02T11:31:00Z")

</div>

2009/11/30 jackster the jackle [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ipconf = `ipconfig`  
> elsif  
> end
> 
> I think I need to somehow combine the “if” and “elsif” statements so I’m  
> not using “or” and this way the ip\_addr variable will hold it’s value?

I would attempt a two stage approach:

1. stage: match a complete adapter description
2. stage: match individual portions of it

str = \<\<‘XXX’  
Ethernet adapter Local Area Connection:

```
   Connection-specific DNS Suffix . : corp.pc.com
   IP Address. . . . . . . . . . . . : 172.1.81.7
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 172.1.81.254

```

Ethernet adapter Network Connect Adapter:

```
   Media State . . . . . . . . . . . : Media disconnected

```

Ethernet adapter Wireless Network Connection:

```
   Connection-specific DNS Suffix . : corp.pc.com
   IP Address. . . . . . . . . . . . : 172.1.17.116
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 172.1.17.254

```

XXX

str.scan %r{  
^ \S [^:]\* : \s\* $  
(?:  
^ \s+ [^:]+ : [^:]+ $ [\n\r]+  
)\*  
}x do |adapter|

# p adapter

name = adapter[%r{Ethernet\s+adapter\s+(\S[^:]_):}, 1]  
addr = adapter[%r{IP Address[^:]_:\s\*(\d+(?:.\d+){3})}, 1]  
printf “%p %p\n”, name, addr  
end

Kind regards

robert
