import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(.*)(-)(.*)(-)(.*)";
final String string = "10-10-2017\n"
+ "10-10-2017\n"
+ "10-10-2017\n"
+ "10-1-2018\n"
+ "10-1-2018\n"
+ "10-1-2018\n"
+ "10-1-2018\n"
+ "10-1-2018\n"
+ "10-1-2018\n"
+ "10-2-2018\n"
+ "10-2-2018\n"
+ "10-2-2018\n"
+ "10-3-2018\n"
+ "10-3-2018\n"
+ "10-7-2017\n"
+ "10-7-2017\n"
+ "10-7-2017\n"
+ "10-7-2017\n"
+ "10-7-2017\n"
+ "10-7-2017\n"
+ "10-7-2017\n"
+ "10-8-2017\n"
+ "10-8-2017\n"
+ "10-8-2017\n"
+ "10-8-2017\n"
+ "10-9-2017\n"
+ "10-9-2017\n"
+ "1-10-2017\n"
+ "1-10-2017\n"
+ "1-10-2017\n"
+ "1-10-2017\n"
+ "11-10-2017\n"
+ "11-10-2017\n"
+ "11-11-2017\n"
+ "11-11-2017\n"
+ "11-11-2017\n"
+ "11-11-2017\n"
+ "1-112017\n"
+ "1-11-2017\n"
+ "1-11-2017\n"
+ "1-11-2017\n"
+ "1-11-2017\n"
+ "1-11-2017\n"
+ "11-1-2018\n"
+ "11-1-2018\n"
+ "11-1-2018\n"
+ "11-1-2018\n"
+ "1-1-2017\n"
+ "1-1-2018\n"
+ "1-1-2018\n"
+ "1-1-2018\n"
+ "11-2-2018\n"
+ "11-2-2018\n"
+ "11-2-2018\n"
+ "11-7-2017\n"
+ "12-11-2017\n"
+ "1-2-2018\n"
+ "1-2-2018\n"
+ "1-2-2018\n"
+ "1-2-2018\n"
+ "12-2-2018\n"
+ "12-2-2018\n"
+ "12-2-2018\n"
+ "12-2-2018\n"
+ "12-2-2018\n"
+ "12-2-2018\n"
+ "12-3-2018\n"
+ "12-3-2018\n"
+ "12-3-2018\n"
+ "12-3-2018\n"
+ "12-3-2018\n"
+ "12-3-2018\n"
+ "12-3-2018\n"
+ "12-7-2017\n"
+ "12-7-2017\n"
+ "12-7-2017\n"
+ "12-7-2017\n"
+ "12-7-2017\n"
+ "12-7-2017\n"
+ "12-7-2017\n"
+ "12-9-2017\n"
+ "13-11-2017\n"
+ "13-11-2017\n"
+ "13-11-2017\n"
+ "13-1-2018\n"
+ "13-1-2018\n"
+ "13-1-2018\n"
+ "1-3-2018\n"
+ "1-3-2018\n"
+ "13-6-2017\n"
+ "13-7-2017\n"
+ "13-7-2017\n"
+ "13-7-2017\n"
+ "13-7-2017\n"
+ "13-7-2017\n"
+ "13-7-2017\n"
+ "13-7-2017\n"
+ "13-8-2017\n"
+ "13-8-2017\n"
+ "13-8-2017\n"
+ "13-8-2017\n"
+ "13-8-2017\n"
+ "13-9-2017\n"
+ "13-9-2017\n"
+ "13-9-2017\n"
+ "14-10-2017\n"
+ "14-10-2017\n"
+ "14-10-2017\n"
+ "14-11-2017\n"
+ "14-11-2017\n"
+ "14-11-2017\n"
+ "14-1-2018\n"
+ "14-1-2018\n"
+ "1-4-2018\n"
+ "1-4-2018\n"
+ "1-4-2018\n"
+ "14-2-2018\n"
+ "14-3-2018\n"
+ "14-3-2018\n"
+ "14-3-2018\n"
+ "14-3-2018\n"
+ "14-3-2018\n"
+ "14-3-2018\n"
+ "14-7-2017\n"
+ "14-8-2017\n"
+ "14-8-2017\n"
+ "14-8-2017\n"
+ "15-10-2017\n"
+ "15-10-2017\n"
+ "15-11-2017\n"
+ "15-11-2017\n"
+ "15-1-2018\n"
+ "15-1-2018\n"
+ "15-1-2018\n"
+ "15-1-2018\n"
+ "15-1-2018\n"
+ "15-1-2018\n"
+ "15-1-2018\n"
+ "15-1-2018\n"
+ "15-1-2018\n"
+ "15-1-2018\n"
+ "15-1-2018\n"
+ "15-1-2018\n"
+ "15-2-2018\n"
+ "15-2-2018\n"
+ "15-2-2018\n"
+ "15-2-2018\n"
+ "15-3-2018\n"
+ "15-3-2018\n"
+ "15-7-2017\n"
+ "15-7-2017\n"
+ "15-7-2017\n"
+ "15-7-2017\n"
+ "15-7-2017\n"
+ "16-10-2017\n"
+ "16-10-2017\n"
+ "16-10-2017\n"
+ "16-10-2017\n"
+ "16-11-2017\n"
+ "16-11-2017\n"
+ "16-11-2017\n"
+ "16-11-2017\n"
+ "16-1-2018\n"
+ "16-7-2017\n"
+ "16-7-2017\n"
+ "16-7-2017\n"
+ "16-8-2017\n"
+ "16-8-2017\n"
+ "16-9-2017\n"
+ "16-9-2017\n"
+ "16-9-2017\n"
+ "16-9-2017\n"
+ "16-9-2017\n"
+ "17-10-2017\n"
+ "17-10-2017\n"
+ "17-10-2017\n"
+ "17-11-2017\n"
+ "17-1-2018\n"
+ "17-1-2018\n"
+ "17-1-2018\n"
+ "17-1-2018\n"
+ "17-1-2018\n"
+ "17-1-2018\n"
+ "17-1-2018\n"
+ "17-1-2018\n"
+ "17-1-2018\n"
+ "17-1-2018\n"
+ "17-1-2018\n"
+ "17-1-2018\n"
+ "17-1-2018\n"
+ "17-1-2018\n"
+ "17-1-2018\n"
+ "17-1-2018\n"
+ "17-1-2018\n"
+ "17-2-2018\n"
+ "17-2-2018\n"
+ "17-2-2018\n"
+ "17-3-2018\n"
+ "17-3-2018\n"
+ "17-3-2018\n"
+ "17-7-2017\n"
+ "17-7-2017\n"
+ "17-7-2017\n"
+ "17-7-2017\n"
+ "17-7-2017\n"
+ "17-7-2017\n"
+ "17-9-2017\n"
+ "17-9-2017\n"
+ "17-9-2017\n"
+ "18-10-2017\n"
+ "18-10-2017\n"
+ "18-10-2017\n"
+ "18-10-2017\n"
+ "18-10-2017\n"
+ "18-11-2017\n"
+ "18-11-2017\n"
+ "18-1-2018\n"
+ "18-1-2018\n"
+ "18-1-2018\n"
+ "18-1-2018\n"
+ "18-1-2018\n"
+ "1-8-2017\n"
+ "1-8-2017\n"
+ "1-8-2017\n"
+ "1-8-2017\n"
+ "18-2-2018\n"
+ "18-3-2018\n"
+ "18-3-2018\n"
+ "18-3-2018\n"
+ "18-7-2017\n"
+ "18-7-2017\n"
+ "18-7-2017\n"
+ "18-7-2017\n"
+ "18-7-2017\n"
+ "18-7-2017\n"
+ "18-9-2017\n"
+ "19-10-2017\n"
+ "19-10-2017\n"
+ "19-10-2017\n"
+ "19-10-2017\n"
+ "19-10-2017\n"
+ "19-11-2017\n"
+ "19-11-2017\n"
+ "19-2-2018\n"
+ "19-2-2018\n"
+ "19-2-2018\n"
+ "19-3-2018\n"
+ "19-3-2018\n"
+ "19-3-2018\n"
+ "19-3-2018\n"
+ "19-3-2018\n"
+ "19-3-2018\n"
+ "19-8-2017\n"
+ "19-8-2017\n"
+ "19-8-2017\n"
+ "19-8-2017\n"
+ "20-1-2018\n"
+ "20-1-2018\n"
+ "20-1-2018\n"
+ "20-1-2018\n"
+ "20-1-2018\n"
+ "20-1-2018\n"
+ "20-1-2018\n";
final String subst = "\\5\\4\\3\\2\\1";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
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