在java中,如何将日志消息与多个字符串模式相匹配

发布于 2024-11-15 19:51:47 字数 461 浏览 3 评论 0原文

我有几个字符串模式:

ArrayList<String> tmp = new ArrayList<String>();
tmp.add("INFO");
tmp.add("Error");
tmp.add("Debug");
tmp.add("Failed");
tmp.add("Unable");

我还在检查文件中的每一行是否与任何一个字符串模式匹配。如果匹配,我将显示该行。我的代码是,

for (String pattern : tmp) {
    if (line.contains(pattern)) {
    System.out.println(line);
    }
}

现在的问题是,如果行与多个字符串模式,每次匹配时都会显示行。

我只想显示该行一次(需要检查任何字符串模式是否与行匹配)。如何做到这一点。

i have several String patterns:

ArrayList<String> tmp = new ArrayList<String>();
tmp.add("INFO");
tmp.add("Error");
tmp.add("Debug");
tmp.add("Failed");
tmp.add("Unable");

also i am checking the every lines in the file whether lines are matched with any one of the string pattern.if matched,i will display the line.my code is,

for (String pattern : tmp) {
    if (line.contains(pattern)) {
    System.out.println(line);
    }
}

Now the problem is,if line match with more than one string pattern,line gets displayed by every time whenever gets matched.

i want to display the line by only one time(need to check any of the string patterns are matched with line).How to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难理解 2024-11-22 19:51:48

使用正则表达式:

Pattern pattern = Pattern.compile("INFO|Error|Debug|Failed|Unable");
for(String line : lines){
    if(pattern.matcher(line).find()){
        System.out.println(line);
    }
}

这将打印包含一个或多个提供的关键字的每一行。

有关详细信息,请参阅正则表达式教程

另外,如果您让正则表达式引擎进行行分割而不是传递单独的行,您可以进一步改进这一点:

Pattern pattern = Pattern.compile("^.*(?:INFO|Error|Debug|Failed|Unable).*$",
                                  Pattern.MULTILINE);
Matcher matcher = pattern.matcher(theWholeSourceText);
while(matcher.find()){
    System.out.println(matcher.group());
}

更新: 好的,如果模式是动态的,您可以从列表中动态构建它:

StringBuilder sb = new StringBuilder();
sb.append("^.*(?:");
Iterator<String> it = patternsList.iterator();
if(it.hasNext())sb.append(it.next());
while(it.hasNext())sb.append('|').append(it.next());
sb.append(").*$");
Matcher matcher = Pattern.compile(sb.toString(), Pattern.MULTILINE)
                         .matcher(theWholeSourceText);
while(matcher.find()){
    System.out.println(matcher.group());
}

Use a regular expression:

Pattern pattern = Pattern.compile("INFO|Error|Debug|Failed|Unable");
for(String line : lines){
    if(pattern.matcher(line).find()){
        System.out.println(line);
    }
}

This will print every line that contains one or more of the supplied keywords.

See the Regular Expression Tutorial for more info.

Also, you could further improve this if you let the regex engine do the line splitting instead of passing individual lines:

Pattern pattern = Pattern.compile("^.*(?:INFO|Error|Debug|Failed|Unable).*$",
                                  Pattern.MULTILINE);
Matcher matcher = pattern.matcher(theWholeSourceText);
while(matcher.find()){
    System.out.println(matcher.group());
}

Update: Ok, if the pattern is dynamic you can just build it dynamically from your list:

StringBuilder sb = new StringBuilder();
sb.append("^.*(?:");
Iterator<String> it = patternsList.iterator();
if(it.hasNext())sb.append(it.next());
while(it.hasNext())sb.append('|').append(it.next());
sb.append(").*$");
Matcher matcher = Pattern.compile(sb.toString(), Pattern.MULTILINE)
                         .matcher(theWholeSourceText);
while(matcher.find()){
    System.out.println(matcher.group());
}
迷途知返 2024-11-22 19:51:47

只需在其中添加一个 break

for (String pattern : tmp) {
 if (line.contains(pattern)) {
   System.out.println(line);
   break;
  }
}

另外,请正确格式化您的代码(缩进 4 个空格),因为这样更容易阅读。

just put a break in there:

for (String pattern : tmp) {
 if (line.contains(pattern)) {
   System.out.println(line);
   break;
  }
}

Also, please properly format your code (indent it with 4 spaces), as it makes it easier to read.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文