我正在从文件中读取输入,其中包含以下几行:
BDI100 172.20.1.5 YES TFTP up up BDI500 172.20.1.50 YES TFTP up up BDI600 172.20.1.58 YES TFTP up up
我必须提取仅包含172.20.1.5的完整行
下面是我的代码:
kubernetes-client
我将输出扩展为:
while ((line = lineNumberReader.readLine()) != null) {
Pattern p = Pattern.compile(expr.trim()); /*expr is filter contains 172.20.1.5 */
Matcher m = p.matcher(line);
if(m.find()){
System.out.println(line);
}
}
但是正在打印所有行。
答案 0 :(得分:2)
这将帮助您:
return 0
答案 1 :(得分:1)
您正在做的事情似乎有点过分。也许试试这个:
String need = "172.20.1.5";
String line = "";
while ((line = lineNumberReader.readLine()) != null) {
if (line.trim().equals("")) {
continue:
}
if (line.contains(need) {
System.out.println(line);
break;
}
}
答案 2 :(得分:0)
/*Intialize expr within braces */
String expr = "(172.20.1.5 )";
while ((line = lineNumberReader.readLine()) != null) {
Pattern p = Pattern.compile(expr.trim()); /*expr is filter contains 172.20.1.5 */
Matcher m = p.matcher(line);
if(m.find()){
System.out.println(line);
}
}