import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(.+)$(?=[\\s\\S]*^(\\1)$[\\s\\S]*)";
final String string = "\n\n"
+ "An HTML attachment was scrubbed...\n"
+ "URL: https://list.something.edu/mailman/attachments/200908002/7452/attachment.html\n"
+ "From SomeAddress at SomeDomain.org SomeDate\n"
+ "From: SomeonesEmail at SomeDomain.org (FirstName LastName)\n"
+ "Date: [the date]\n"
+ "Subject: [ListservName] SomeSubject\n"
+ "In-Reply-To: <SomeIDnumber>\n"
+ "References: <%SomeEmailAddress>\n"
+ " <SomeIDnumber>\n"
+ "Message-ID: <SomeIDnumber>\n\n"
+ "Hey there, Everyone,\n\n"
+ "I completely disagree with this. It's crap!\n\n"
+ "Sincerely,\n"
+ "SomePerson\n\n\n\n"
+ "On SomeDate, Someone wrote:\n"
+ "From: SomeonesEmail at SomeDomain.org (FirstName LastName)\n"
+ "Date: [the date]\n"
+ "Subject: [ListservName] SomeSubject\n"
+ "In-Reply-To: <SomeIDnumber>\n"
+ "References: <%SomeEmailAddress>\n"
+ " <SomeIDnumber>\n"
+ "Message-ID: <SomeIDnumber>\n\n"
+ "Hey PersonA,\n\n"
+ "This is my advice. It is really good advice. I hope you take it since I think it's pertinent.\n\n"
+ "Sincerely,\n"
+ "PersonZ\n\n\n\n\n"
+ "An HTML attachment was scrubbed...\n"
+ "URL: https://list.something.edu/mailman/attachments/200908002/7452/attachment.html\n"
+ "From SomeAdress at SomeDomain.org SomeDate\n"
+ "From: SomeonesEmail at SomeDomain.org (FirstName LastName)\n"
+ "Date: [the date]\n"
+ "Subject: [ListservName] SomeSubject\n"
+ "In-Reply-To: <SomeIDnumber>\n"
+ "References: <%SomeEmailAddress>\n"
+ " <SomeIDnumber>\n"
+ "Message-ID: <SomeIDnumber>\n\n"
+ "Good Afternoon, PersonA,\n\n"
+ "But have you considered this aspect? It changes everything, so this is my advice. It is even better advice! \n\n"
+ "Sincerely,\n"
+ "PersonB\n\n\n\n"
+ "On SomeDate, PersonZ wrote:\n\n"
+ "Hey PersonA,\n\n"
+ "This is my advice. It\n"
+ "is really\n"
+ "good advice. I hope you take it since I\n"
+ "think it's pertinent.\n\n"
+ "Sincerely,\n"
+ "PersonZ\n\n\n\n"
+ "From SomeAddress at SomeDomain.org SomeDate \n"
+ "From: SomeonesEmail at SomeDomain.org (FirstName LastName)\n"
+ "Date: [the date]\n"
+ "Subject: [ListservName] SomeSubject\n"
+ "In-Reply-To: <SomeIDnumber>\n"
+ "References: <%SomeEmailAddress>\n"
+ " <SomeIDnumber>\n"
+ "Message-ID: <SomeIDnumber>\n\n"
+ "Thank you all for your feedback on this issue. You've given me a lot to\n"
+ "consider. Cheers.\n\n\n\n"
+ "[And so on.]\n\n";
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