Filtering returned results
Hi,
I have added a small piece of code that goes out and discovers what ethernet ports are available on a PC and returns them to the calling function in a GUI that populates a Combo Box. The code is as follows:
Code:
import java.net.*;
import java.net.SocketException;
import java.util.*;
public class ListNets
{
String tmp2[];
public static String[] ListEthPorts ()
{
String tmp[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"};
int i = 0;
try
{
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets))
{
tmp[i] = netint.getName();
i++;
}
}
catch(SocketException exp)
{
}
return tmp;
}
}
This works great and does actually populate the combo box with the correct interface names. What I want to do now is apply a filter based on a regular expression that only returns interface names of a specific type such as eth1.34, eth1.35 etc.... Currently it returns all interfaces such as l0, vif 1.1.
The regular expression I want to apply is:
Code:
"(eth[0-9]\\.[0-9]{2})|(eth[0-9])|(eth[0-9]{2})|(eth[0-9]{2}\\.[0-9])|(eth[0-9]{2}\\.[0-9]{2})"
Is there a neat way of doing this just before tmp is returned? I have tried some examples that are out there but they have not worked for me. Also is there a neater way to initialize the 12 ports instead of the way I have done it with the String tmp[]?
Thanks
Eric
BTW, I am a complete newb at Java, so forgive my ignorance in advance.
Re: Filtering returned results
What contents do you need in the tmp array? The code replaces the first n values.
Re: Filtering returned results
Hi Norm,
Each PC I am running the GUI on has a combination of real, virtual and sub-interfaces. I have VLAN trunking setup and the GUI I want to run only on the sub-interfaces that connect to the VLan'd switch. Basically there are 12 interfaces and that code returns the following for example on one PC:
"vif0, peth0, eth1, eth1.35, eth1.34, eth1.33, eth1.32, eth1.31, eth1.30, eth0, virbr0, l0"
I only want to return "eth1.35, eth1.34, eth1.33, eth1.32, eth1.31, eth1.30"
The regular expression I constructed above will work but I can't figure how to apply it. I can get the required results by indexing the results but that means if I add or remove sub-interfaces, I have to alter the code. The best way is to filter using the regular expression.
Thanks
Re: Filtering returned results
Is there a forum for regular expressions?
Re: Filtering returned results
Take a look here for regular expressions in java. Basically, i think you'd just want to throw an if statement around where you assign the name to the string array and check the string with the .matches() function.
Code:
if (netint.getName().matches(".*")) {
...add to array...
}
Also, instead of using a string array whose length has to be defined, i would suggest looking at an ArrayList. ArrayLists are dynamic so you can add as many values as you want whenever you need to.
Re: Filtering returned results
Shoss,
Thanks for the pointer to ArrayLists. I applied the following code and it works as expected.
Code:
import java.lang.String.*;
import java.net.*;
import java.net.SocketException;
import java.util.*;
import java.util.ArrayList.*;
import java.util.regex.Pattern;
public class ListNets
{
public static String[] ListEthPorts ()
{
String [] tmp = null;
String ifName = null;
ArrayList<String> ifList = new ArrayList<String>();
try
{
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets))
{
ifName = netint.getName();
if(Pattern.matches("(eth[0-9]\\.[0-9]{2})|(eth[0-9]{2}\\.[0-9])|(eth[0-9]{2}\\.[0-9]{2})", ifName) == true)
{
ifList.add(ifName);
}
}
int numValidIf = ifList.size();
tmp = new String[numValidIf];
for(int i = 0; i < numValidIf; i++)
{
tmp[i] = ifList.get(i);
}
}
catch(SocketException exp)
{
}
return tmp;
}
}
Eric