\B), this captures till end of word

问题描述 投票:1回答:1

Java demo

[C] L1250 WARN  k2 bw34 Flex - Sockets:<16>, ThreadsPerCore:<1>
[C] L1250 WARN  For abcd (analytical and transactional workloads). For 12s Systems and above, should be
                disabled.
[C] L1250 INFO  For abcd (analytical workloads), Hyperthreading should be enabled , 8s, 12s, 14d, 34t
                d above.
[C] L1250 WARN  Intel's Hyperthreading on 18+ Socket system disabled. Should be disabled urgently
                fix it!
[C] L1300 OK    CPU governors set as recommended
[C] L1250 WARN  Intel's Hyperthreading on 8+ Socket system disabled.

For example:

Output

enter image description here

If the
java regex regex-lookarounds multiline regex-greedy
1个回答
2
投票

or [\s\S]*Regex demo[C]

\bWARN\h+.*(?:\R(?!\[C]).*)*

In Java

  • \bWARN
  • \h+.*
  • (?:Try this regex with option
    • \R(?!\[C]).*global[C]
  • )* and

single line: This will find everything starting with WARN until the next '[C]' or the end of the input string.

Demo:

String regex = "\\bWARN\\h+.*(?:\\R(?!\\[C]).*)*";
String string = "[C] L1250 WARN  k2 bw34 Flex - Sockets:<16>, ThreadsPerCore:<1>\n"
     + "[C] L1250 WARN  For abcd (analytical and transactional workloads). For 12s Systems and above, should be\n"
     + "                disabled.\n"
     + "[C] L1250 INFO  For abcd (analytical workloads), Hyperthreading should be enabled , 8s, 12s, 14d, 34t\n"
     + "                d above.\n"
     + "[C] L1250 WARN  Intel's Hyperthreading on 18+ Socket system disabled. Should be disabled urgently\n"
     + "                fix it!\n"
     + "[C] L1300 OK    CPU governors set as recommended\n"
     + "[C] L1250 WARN  Intel's Hyperthreading on 8+ Socket system disabled.";

Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    System.out.println(matcher.group(0));
}

https: 一般来说,WARN可能是或不是多行的,如下图所示。

WARN  k2 bw34 Flex - Sockets:<16>, ThreadsPerCore:<1>
WARN  For abcd (analytical and transactional workloads). For 12s Systems and above, should be
                disabled.
WARN  Intel's Hyperthreading on 18+ Socket system disabled. Should be disabled urgently
                fix it!
WARN  Intel's Hyperthreading on 8+ Socket system disabled.

最初,我用regex开始。(WARN).*(\bnon字边界,它没有捕获下面的多行(继续WARN描述)。[C]然后我试了->WARN.+([\S\s]*?)+(?=\[C\]),但这不能捕获最后一行WARN,因为没有进一步的[C]标记。WARN INFO OK

 \bWARN\h+.*(?:\R(?!.*\h(?:WARN|INFO|OK)\h).*)*

我试图为输入文本写一个正则表达式,我必须提取前面所有的WARN代码和信息。一般来说,可能是或可能不是多行,如下图所示。[C] L1250 WARN k2 ...

你可以不使用

String regex = "\\bWARN\\h+.*(?:\\R(?!.*\\h(?:WARN|INFO|OK)\\h).*)*";
或单行选项,匹配所有不以

1
投票

在WARN之前加上一个词的边界,以防止成为一个更大的词的一部分。 匹配1个以上的水平空格字符 非捕获组 匹配unicode换行序列,断言该字符串不以 关闭小组,重复0+次WARN.*?(?=\[C\]|$)

Regex演示

/regex101.comrKZXWwL1

© www.soinside.com 2019 - 2024. All rights reserved.