Regular Expression Problem

S

Stevo

I'm trying to parse the output of ipconfig /all to retrieve the first MAC address that is not part of a VM adapter and the second byte should not be 50. A sample return is as follows: I'm able to retrieve just the MAC addresses and I'm able to discard them if 50 is in the second byte, but how do I determine if the word 'VMware' is in the immediately preceding description field and if so, enforce that no match is made.

Thanks.

Regular Expression:
..{2}-(?!50).{2}-.{2}-.{2}-.{2}-.{2}



Windows IP Configuration

Host Name . . . . . . . . . . . . : ws-remote
Primary Dns Suffix . . . . . . . : nothing.com
Node Type . . . . . . . . . . . . : Unknown
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
DNS Suffix Search List. . . . . . : nothing.com

Ethernet adapter VMware Network Adapter VMnet8:

Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for
VMnet8
Physical Address. . . . . . . . . : 00-50-56-C0-00-08
Dhcp Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 192.168.222.1
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :

Ethernet adapter VMware Network Adapter VMnet1:

Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for
VMnet1
Physical Address. . . . . . . . . : 00-50-56-C0-00-01
Dhcp Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 192.168.17.1
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :

Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : SiS 900 PCI Fast Ethernet Adapter
Physical Address. . . . . . . . . : 00-0A-E6-AD-4B-B7
Dhcp Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
IP Address. . . . . . . . . . . . : 192.168.0.200
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.0.1
DHCP Server . . . . . . . . . . . : 192.168.0.10
DNS Servers . . . . . . . . . . . : 192.168.0.10
Lease Obtained. . . . . . . . . . : Sunday, November 02, 2003 8:44:31 AM

Lease Expires . . . . . . . . . . : Monday, November 10, 2003 8:44:31 AM
 
C

Chris R. Timmons

I'm trying to parse the output of ipconfig /all to retrieve the
first MAC address that is not part of a VM adapter and the
second byte should not be 50. A sample return is as follows:
I'm able to retrieve just the MAC addresses and I'm able to
discard them if 50 is in the second byte, but how do I determine
if the word 'VMware' is in the immediately preceding description
field and if so, enforce that no match is made.

Stevo,

You can get both the Description and Physical Address w/ one regex.

string re =
@"Description.*?:\s*(?<description>.*?)\r\n" +
".*?Physical Address.*?:\s*(?<MAC>.*?)\r\n";

// "text" contains the Windows IP Config text.
MatchCollection mc = Regex.Matches(text, re,
RegexOptions.Singleline);

string description;
string MAC;
foreach(Match m in mc)
{
description = m.Groups["description"].ToString();
MAC = m.Groups["MAC"].ToString();

if (Regex.IsMatch(description, "vmware", RegexOptions.IgnoreCase))
continue;

Console.WriteLine(description);
Console.WriteLine(MAC);
}


In a DOS prompt, this will print:

SiS 900 PCI Fast Ethernet Adapter
00-0A-E6-AD-4B-B7


Hope this helps.

Chris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top