我有一句话:
ERROR: file' user\username\file\myfile.mp3' c
annot be used, because required software is not installed.
Follow the given instruction below
instruction one.............
我想要一个涵盖
的正则表达式first two line that means from "ERROR" to "is not installed".
我使用下面的
(ERROR\:[^\.]+\.?)
但只需
ERROR: file' user\username\file\myfile
任何形式的帮助将不胜感激,谢谢
答案 0 :(得分:3)
答案 1 :(得分:1)
如果我们可以依赖于所需消息之后存在空行的格式,我们可以使用它来捕获消息本身:
/ERROR\:[\W\w]*?(?=\r?\n\r?\n)/
ERROR\:
文字文字。[\W\w]*?
匹配任何字符零次或多次,但尽可能少。这意味着它将与下一步应该匹配。(?=\r?\n\r?\n)
如果以下字符组成一个空行,则匹配。 (但不包括该文本作为比赛本身的一部分。)答案 2 :(得分:0)
public static void main(String[] args) {
String line = "ERROR: file' user\\username\\file\\myfile.mp3' c\n"+
"annot be used, because required software is not installed.\n\n"+
"Follow the given instruction below\n"+
"instruction one.............\n";
String regex = "^ERROR: [A-Za-z0-9\\.,'\\s\\\\\n]+(?=\\s{2})";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(line);
if(matcher.find()) {
System.out.println(line);
System.out.println(matcher.group(0) );
}
}
你会得到:
ERROR: file' user\username\file\myfile.mp3' c
annot be used, because required software is not installed.
Follow the given instruction below
instruction one.............
ERROR: file' user\username\file\myfile.mp3' c
annot be used, because required software is not installed.
^ERROR: [A-Za-z0-9\\.,'\s\\\n]+(?=\s{2})
请在此处查看结果:https://regex101.com/r/sL8vQ3/1