import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "\\n\\s\\sname:\\sI_LOGON_AUTH_FAILED\\n[\\s\\S]*?\\s\\ssession\\n[\\s\\S]*?\\s\\s\\s\\sremoteAddress:\\s(?<ipaddress>.*?):[0-9]*\\n[\\s\\S]*?\\s\\sauthentication\\n[\\s\\S]*?\\s\\s\\s\\suserName:\\s(?<username>.*)";
final String string = "event\n"
+ " time: 2021-11-23 22:42:59.164039 +0300\n"
+ " app: BvSshServer 8.49\n"
+ " name: I_LOGON_AUTH_FAILED\n"
+ " desc: User authentication failed.\n"
+ " session\n"
+ " id: 1015\n"
+ " service: SSH\n"
+ " remoteAddress: 192.168.0.103:41104\n"
+ " authentication\n"
+ " attemptNr: 1\n"
+ " serialize: completion\n"
+ " userName: userTest\n"
+ " method: keyboard-interactive\n"
+ " submethod: Password\n"
+ " windowsAccount: DESKTOP-FN3LTFE\\paprikar\n"
+ " parameters\n"
+ " failureReason: BadCredentials\n"
+ " help\n"
+ " message: Unknown user name or incorrect password.\n";
final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
final Matcher matcher = pattern.matcher(string);
if (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html