import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "([\\w-]+): (.+?)\\\\r\\\\n(?:([^:]+?)\\\\r\\\\n)?";
final String string = "MIME-Version: 1.0\\r\\n\n"
+ "x-no-auto-attachment: 1\\r\\n\n"
+ "Received: by 10.200.36.132; Sun, 5 Feb 2017 01:21:33 -0800 (PST)\\r\\n\n"
+ "Date: Sun, 5 Feb 2017 01:21:33 -0800\\r\\n\n"
+ "Message-ID: <IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII@mail.gmail.com>\\r\\n\n"
+ "Subject: =?UTF-8?Q?MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM?=\\r\\n\n"
+ "=?UTF-8?Q?ail?=\\r\\n \\\n"
+ "From: =?UTF-8?B?VGhlIGZ1Y2sgYXJlIHUgbG9va2luZyBmb3I/?= <noreply@mail.com>\\r\\n\n"
+ "To: mail mail <mail@mail.com>\\r\\n\n"
+ "Content-Type: multipart/alternative; boundary=1a3xca651sv561fd321c5xv61sd12\\r\\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