How to find specific values in a String?

Asked

Viewed 209 times

3

I have to get the Ethernet adapter + IPV4 from Windows, until then everything is quiet. I ran the process and stored everything in a String Buffer and sent it back:

public String IP (String tarefa)
{           
    StringBuffer buffer = new StringBuffer();

    try {
        Process processo = Runtime.getRuntime().exec(tarefa);
        InputStream fluxo = processo.getInputStream();
        InputStreamReader lefluxo = new InputStreamReader(fluxo);

        BufferedReader bufferLeitura = new BufferedReader(lefluxo);
        String linha = bufferLeitura.readLine();

        while(linha != null)
        {
            buffer.append(linha);
            buffer.append("\n");                                
            linha = bufferLeitura.readLine();
        }
    } catch (IOException e){
        e.printStackTrace();
    }

    return buffer.toString();
}

Now, I have to pick up only the network adapter and IP number to present as a result and I’m not able to think of a way to do such a thing.

How do I divide the String into the parts I want? And if it is possible to do this on Main?

  • 5

    Have you tried using regular expressions? I’m without java here to see what the string looks like, please post it.

  • 4

    It would be much easier to put the string you want to split, the source code doesn’t help much, for now.

1 answer

2

I’m considering you’re running ipconfig, for this case. On my machine while executing your code this was the return of your code:

Windows IP Configuration


Ethernet adapter VirtualBox:

   Connection-specific DNS Suffix  . : 
   Link-local IPv6 Address . . . . . : fe80::bd60:54a2:6750:bd19%17
   Autoconfiguration IPv4 Address. . : 169.254.189.25
   Subnet Mask . . . . . . . . . . . : 255.255.0.0
   Default Gateway . . . . . . . . . : 

Ethernet adapter Ethernet:

   Connection-specific DNS Suffix  . : 
   Link-local IPv6 Address . . . . . : fe80::582:c128:49ba:f141%2
   IPv4 Address. . . . . . . . . . . : 192.168.1.100
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.1.1

Ethernet adapter Bluetooth Network Connection:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : 

Tunnel adapter isatap.{D19E2903-0FED-491B-A030-6B12CB30F3C3}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : 

Tunnel adapter Teredo Tunneling Pseudo-Interface:

   Connection-specific DNS Suffix  . : 
   IPv6 Address. . . . . . . . . . . : 2001:0:5ef5:79fd:2cd2:ee13:42fb:b44b
   Link-local IPv6 Address . . . . . : fe80::2cd2:ee13:42fb:b44b%19
   Default Gateway . . . . . . . . . : ::

Tunnel adapter isatap.{0D84F8B4-2DA0-4513-9AF4-700EDF9BA40F}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : 

One way, as already said, is using regular expressions. In this case I will need to use two:

  • one to find the adapter, considering lines that start with Ethernet adapter, something like that:
(Ethernet adapter )(\w*)
  • and another to retrieve the IP itself, considering those that have IPv4 Address, something like this:
(IPv4 Address)(\. |\: )*(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4})

I created an entity just to make it easier to assemble the return, I named it NetworkInfo and she like this:

public class NetworkInfo {

    private String ip;
    private String adapter;

    // getters e setter

    @Override
    public String toString() {
        return String.format("Adapter: %s | IP: %s", this.getAdapter(), this.getIp());
    }

}

From the result generated by your method IP, we will now go in the resulting string (the one above) search for the content that interests us. Not to be necessary to do match in lines without content I am disregarding them (if (linha.trim().length() > 0)). See below for an example of a method that processes the String generated by IP (ipconfig in my case):

private static final Pattern ADAPTER_PATTERN = Pattern.compile("(Ethernet adapter )(\\w*)");
private static final Pattern IPV4_PATTERN = Pattern.compile("(IPv4 Address)(\\. |\\: )*(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.|$)){4})");

public List<NetworkInfo> listNetworkAdapters(final String task) throws Exception {
    final List<NetworkInfo> result = new ArrayList<>();

    String ip = null;
    String adapter = null;

    final String ipconfig = this.ipconfig(task); // "ipconfig" é o seu método "IP".

    final String[] lines = ipconfig.split("\n");

    for (final String line : lines) {
        if (line.trim().length() > 0) {
            final Matcher adapterMatcher = ADAPTER_PATTERN.matcher(line);
            if (adapterMatcher.find()) {
                adapter = adapterMatcher.group(2); // recuperamos apenas o nome, o 2º grupo da regex
            } else {
                final Matcher ipMatcher = IPV4_PATTERN.matcher(line);
                if (ipMatcher.find()) {
                    ip = ipMatcher.group(3); // aqui recuperamos apenas o IP, o 3º grupo da regex
                }
            }

            if (ip != null && adapter != null) {
                final NetworkInfo info = new NetworkInfo();
                info.setIp(ip);
                info.setAdapter(adapter);
                result.add(info);

                ip = null;
                adapter = null;
            }
        }
    }
    return result;
}

Here, as you can see, I’m creating a array for each divine returned line to String by line break, \n. After this we try to look for the patterns we need and when we have a double formed, we create a NetworkInfo and add to the return.

From the information obtained by ipconfig above, two objects were generated NetworkInfo as a return by the method listNetworkAdapters. Making their impression like this:

public static void main(final String[] args) throws Exception {
    final List<NetworkInfo> result = new IPConfig().listNetworkAdapters("ipconfig");
    for (final NetworkInfo info : result) {
        System.out.println(info);
    }
}

The result generated was this:

Adapter: VirtualBox | IP: 169.254.189.25
Adapter: Ethernet | IP: 192.168.1.100

Remember that the example is considering only this return, if your pattern is different, update your question with the result you get.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.