import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?P<datetime>\\d{2}\\/\\d{2}\\/\\d{2} \\d{2}:\\d{2}:\\d{2}): (?P<name>\\w+(?::\\s*\\w+)*|[\\w\\s]+?)(?:\\s+(?P<action>joined|left|was removed|changed the (?:subject to “\\w+â€|group icon))|:\\s(?P<message>(?:.+|\\n(?!\\n))+))";
final String string = "29/03/14 15:48:05: John Smith changed the subject to “Testâ€\n\n"
+ "29/03/14 16:10:39: John Smith joined\n\n"
+ "29/03/14 16:10:40: Person:2 joined\n\n"
+ "29/03/14 16:10:40: John Smith: Hello!\n\n"
+ "29/03/14 16:11:40: Person:2: some random words,\n\n"
+ "29/03/14 16:12:40: Person3 joined\n\n"
+ "29/03/14 16:13:40: John: Smith: Hello!Test message with newline\n"
+ "Another line of the same message\n"
+ "Another line.\n\n"
+ "29/03/14 16:14:43: Person:2: Test message using as last word joined\n\n"
+ "29/03/14 16:15:57: Person3 left\n\n"
+ "29/03/14 16:17:16: Person3 joined\n\n"
+ "29/03/14 16:18:21: Person:2 changed the group icon\n\n"
+ "29/03/14 16:19:16: Person3 was removed \n\n"
+ "29/03/14 16:20:43: Person:2: Test message using as last word left";
final Pattern pattern = Pattern.compile(regex);
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