Search code examples
regexjava-6

Regex instead of string processing


I have following output from a command:

 24936 09/23/18 23:19:53 --------------------------------------------------
 24936 09/23/18 23:19:53                INITIALISING NETWORK PARAMETERS
 24936 09/23/18 23:19:53 --------------------------------------------------
 24936 09/23/18 23:19:53 Current Executable directory is C:\CVS\Base
 24936 09/23/18 23:19:53 Checking dBaseHome in the Registry for Instance002
 24936 09/23/18 23:19:53 C:\CVS\Base
 24936 09/23/18 23:19:53      Network Parameters
 24936 09/23/18 23:19:53     --------------------
 24936 09/23/18 23:19:53 Host Name              : 10-43-96-175
 24936 09/23/18 23:19:53 Domain Name            : abcd.com
 24936 09/23/18 23:19:53 DNS Server IP          : 12.43.53.23
 24936 09/23/18 23:19:53 Machine uses Dynamic Host configuration Protocol
 24936 09/23/18 23:19:53 No of Interfaces : 22
 24936 09/23/18 23:19:53  Initialization Completed
 --------------------------------------------------
                Initialization Result
--------------------------------------------------
Client Host Names               :lkmn.com
Client Names                    :client
CommServeHostName               :host.com
Configuration                   :The machine is Client
IP Configured                   :IPv4
Instance Name                   :Instance002
Networking Status               :Passed
--------------------------------------------------
                    LOG BEGINS
--------------------------------------------------
24936 09/23/18 23:19:53 --------------------------------------------------
 24936 09/23/18 23:19:53  DNS lookup for Host Name : abcd.com
 24936 09/23/18 23:19:53 --------------------------------------------------
 24936 09/23/18 23:19:53 DNS Look Up Output:
 Non-authoritative answer:
Server:  server.com
Address:  43.4.3.4

Name:    client.com
Address:  10.43.96.175

 24936 09/23/18 23:19:53 --------------------------------------------------
 24936 09/23/18 23:19:53 --------------------------------------------------
 24936 09/23/18 23:19:53 Using IPv4 family
 24936 09/23/18 23:19:53 Testing Addresses:
 24936 09/23/18 23:19:53 Testing 10.43.96.175 ->
 24936 09/23/18 23:19:53 [Failed]

 Failure reason : Generic Failure
--------------------------------------------------
                 SUMMARY
 --------------------------------------------------
 Forward and Reverse Lookup - CVIPInfo :

 IP : 10.43.96.175 Failed
 --------------------------------------------------
                    LOG ENDS
--------------------------------------------------

       RESULTS
       -------
 DNS LOOKUP       : SUCCESS
 HOST FILE LOOKUP : NOT PRESENT

 FORWARD AND REVERSE LOOKUP
 --------------------------
 IP Version : IPv4
 Status     : FAILED
____________________________________________________________
                           END
____________________________________________________________

I want to extract the value of key CommServeHostName from it which is host.com.

I have tried following code by splitting the lines:

for (String line : output.split("\n")) {
  if (line.startsWith("CommServeHostName")) {
    String[] split = line.split(":");
    System.out.println(split.length > 1 ? split[1] : null);
    break;
  }
}

Is there a better way to do it without splitting the output to lines and using regex? It has to be done in Java-6.


Solution

  • You can use the regex

    (?m)^CommServeHostName\\s+(.+)$
    

    which will match the line that begins with CommServeHostName and will capture whatever non-spaces there are right before the end of the line in the first capturing group:

    String reg = "(?m)^CommServeHostName\\s+(.+)$";
    Matcher m = Pattern.compile(reg).matcher(output);
    m.find();
    System.out.println(m.group(1));