import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(.+?)\\. Last message: (.+\\.?) (\\w[^AMP].+?)\\.$";
final String string = "Sandy Smith. Last message: Will do! 2/2/17.\n\n"
+ "Caroline Doe. Last message: I will definitely tell him to come! Thank you so much for checking up on the both of us 2/2/17.\n\n\n"
+ "thundercall@ecnetwork.com. Last message: (Alert) CodeRED Weather Warning: The NWS has issued a Severe Thunderstorm Warning for your WALTHAM ST location from 8:00 AM April 30 until 8:30 AM April 3 4/30/16.\n\n"
+ "BFristN BLastN. Last message: Okay, I figured it out. Will post on the forum in a sec. 4:25 PM.\n\n"
+ "BFristN BLastN. Last message: Okay, I figured it out. Will post on the forum in a sec. Yesterday.\n\n\n\n";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
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