import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?=\\[\\d\\d.\\d\\d.\\d\\d, \\d\\d:\\d\\d\\])";
final String string = "[29.01.18, 23:33] Alice: Ist das hier ein Chatverlauf?\\n[29.01.18, 23:45] Bob: Ja ist es!\\n[29.01.18, 23:45] Bob: Der ist dazu da die funktionsweise des Parsers zu demonstrieren\\n[29.01.18, 23:46] Alice: PTT-20180129-WA0025.opus (Datei angehängt)\\n[29.01.18, 23:46] Bob: Ah, er kann also auch erkennen ob Voicemails gesendet wurden!\\n[29.01.18, 23:46] Bob: Das ist praktisch!\\n[29.01.18, 23:47] Bob: Oder?\\n[29.01.18, 23:47] Alice: ja😄\\n[29.01.18, 23:47] Alice: und Emojis gehen auch!\\n[29.01.18, 23:47] Bob: Was ist mit normalen Smilies?\\n[29.01.18, 23:49] Alice: Keine Ahnung, lass uns das doch mal ausprobieren\\n[29.01.18, 23:50] Bob: Alles klar :) :D\\n[29.01.18, 23:51] Alice: Scheint zu funktionieren!:P\\n[29.01.18, 23:51] Bob: Meinst du, dass URLS auch erkannt werden?\\n[29.01.18, 23:52] Bob: Schick doch mal eine zum ausprobieren!\\n[29.01.18, 23:53] Alice: https://github.com/JuKo007\\n[29.01.18, 23:58] Alice: Scheint zu funktionieren!\\n[29.01.18, 23:59] Alice: Sehr schön!\\n[30.01.18, 00:00] Alice: Damit sollten sich WhatsApp Verläufe besser quantifizieren lassen!\\n[30.01.18, 00:02] Bob: Alles klar, los gehts 😌\\n";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (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