import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "System Message: (?<agent>\\w+) is ready to chat.*?[\\r\\n]+[\\d\\/]+\\s+[\\d:]+\\s+\\w+\\s+[\\r\\n]+(?P=agent)[\\r\\n]+(?<agentFirstResponse>.*?)[\\r\\n]+[\\d\\/]+\\s+[\\d:]+\\s+\\w+";
final String string = "1/1/2019 2:42:55 AM \n"
+ "Aju \n"
+ "Hi Team\n\n"
+ "1/1/2019 2:42:56 AM \n"
+ "System \n"
+ "The data has been added: \n"
+ "- Customer Info\n\n"
+ "1/1/2019 2:42:59 AM \n"
+ "Rohi\n"
+ "System Message: Rohi is ready to chat. \n\n"
+ "1/1/2019 2:43:09 AM \n"
+ "Aju \n"
+ "Wish you a very happy ne year \n\n"
+ "1/1/2019 2:43:12 AM \n"
+ "Aju \n"
+ "new* \n\n"
+ "1/1/2019 2:43:25 AM \n"
+ "Aju \n"
+ "I need to KNOW ABOUT A CAR\n\n"
+ "1/1/2019 2:43:32 AM \n"
+ "Aju \n"
+ "please help me \n\n"
+ "1/1/2019 2:45:07 AM \n"
+ "Aju \n"
+ "Anyone there ? \n\n"
+ "1/1/2019 2:47:13 AM \n"
+ "Aju \n"
+ "?? \n\n"
+ "1/1/2019 2:49:23 AM \n"
+ "Aju \n"
+ "?? Hi Rohi You there? \n\n"
+ "1/1/2019 2:51:16 AM \n"
+ "Rohi\n"
+ "Hello Aju my name is Rohi. How can I help you today? \n\n"
+ "1/1/2019 2:51:27 AM\n\n"
+ "Chat goes on....";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.DOTALL);
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