Results 1 to 6 of 6
Thread: Filtering returned results
- 01-24-2012, 12:04 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
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:
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.Java 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; } }
The regular expression I want to apply is:
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[]?Java 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})"
Thanks
Eric
BTW, I am a complete newb at Java, so forgive my ignorance in advance.Last edited by eohalloran; 01-24-2012 at 12:14 AM. Reason: Clarification that Iam new to Java
- 01-24-2012, 01:21 AM #2
Re: Filtering returned results
What contents do you need in the tmp array? The code replaces the first n values.
- 01-24-2012, 03:16 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
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
- 01-24-2012, 04:00 PM #4
Re: Filtering returned results
Is there a forum for regular expressions?
- 01-24-2012, 05:43 PM #5
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.
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.Java Code:if (netint.getName().matches(".*")) { ...add to array... }
- 01-25-2012, 05:14 PM #6
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
Re: Filtering returned results
Shoss,
Thanks for the pointer to ArrayLists. I applied the following code and it works as expected.
EricJava 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; } }
Similar Threads
-
Filtering an array
By counterfox in forum New To JavaReplies: 7Last Post: 01-19-2012, 03:47 AM -
Filtering strings?
By Gradden in forum New To JavaReplies: 1Last Post: 09-26-2011, 04:18 AM -
Null-Object returned though returned object is working
By Boreeas in forum New To JavaReplies: 5Last Post: 09-17-2011, 01:35 AM -
JLists - Filtering
By Psyclone in forum AWT / SwingReplies: 1Last Post: 02-16-2010, 06:09 AM -
looping and filtering
By javafanatic in forum New To JavaReplies: 14Last Post: 02-09-2010, 09:48 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks