我从命令中得到以下输出:
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完成。
答案 0 :(得分:1)
您可以使用正则表达式
(?m)^CommServeHostName\\s+(.+)$
将与以CommServeHostName
开头的行匹配,并在第一个捕获组的行末之前捕获所有非空格:
String reg = "(?m)^CommServeHostName\\s+(.+)$";
Matcher m = Pattern.compile(reg).matcher(output);
m.find();
System.out.println(m.group(1));
答案 1 :(得分:0)
尝试检查匹配的行,然后提取所需的部分:
if (line.matches("CommServeHostName\\s+:.*")) {
String value = line.replaceAll("^.*:", "");
System.out.println(value);
}