Based on a bunch of replies here: Capturing Part of String and Using Java to find substring of a bigger string using Regular Expression
I'm trying to get multiple parts of a string as substrings:
import java.io.File;
import java.util.regex.*;
String subject = "Re: New Mail File Alert: MAIL_20140320_0000000002.dat XYZ";
Pattern p = Pattern.compile("^(Re|Fwd?)(.*)(New Mail File Alert: )(MAIL_)((20)\\d\\d)(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])[_](\\d{10})(\\.)(dat|ctl)(\\s)(XYZ|ABC)$");
Matcher m = p.matcher(subject);
if (m.find()){
String file = subject.replaceAll("(MAIL_)((20)\\d\\d)(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])[_](\\d{10})", "$1");
String path = subject.replaceAll("(XYZ|ABC)$", "$1");
}
The bits I want are: "MAIL_20140320_0000000002" and "XYZ" however the strings I'm getting back are:
file: Re: New Mail File Alert: MAIL_.dat XYZ
path: Re: New Mail File Alert: MAIL_20140320_0000000002.dat XYZ
Can anyone see what I'm doing wrong here?