正则表达式而不是字符串处理

Regex instead of string processing

我有以下命令输出:

 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
____________________________________________________________

我想从中提取键 CommServeHostName 的值 host.com

我尝试通过拆分行来执行以下代码:

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

有没有更好的方法来做到这一点而不将输出拆分为行并使用正则表达式?必须在 Java-6.

内完成

尝试检查匹配行,然后提取您想要的部分:

if (line.matches("CommServeHostName\s+:.*")) {
    String value = line.replaceAll("^.*:", "");
    System.out.println(value);
}

您可以使用正则表达式

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

将匹配以 CommServeHostName 开头的行,并将捕获第一个捕获组中行末尾之前的所有非空格:

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