import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^subject:\\s(.+)";
final String string = "Received: from DOMAIN.mydomain.com (UnknownHost [127.0.0.1]) by DOMAIN.mydomain.net with SMTP;\n"
+ " Fri, 6 Sep 2013 10:34:07 -0600\n"
+ "Date: Fri, 6 Sep 2013 10:34:07 -0600 (MDT)\n"
+ "From: \"MyDomain.com\" \n"
+ "To: test@anotherdomain.com\n"
+ "Message-ID: <8279725.100.1378485247161.JavaMail.MYDOMAIN$@127.0.0.1>\n"
+ "Subject: Membership Activation\n"
+ "MIME-Version: 1.0\n"
+ "Content-Type: text/html; charset=UTF-8\n"
+ "Content-Transfer-Encoding: 7bit\n"
+ "Subject: Membership Activation\n";
final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
if (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