import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(\\d+)-(\\d+)\\s+(.*)";
final String string = "3-01 Wait for It.mp4\n"
+ "3-02 We're Not from Here.mp4\n"
+ "3-03 Third Wheel.mp4\n"
+ "3-04 Little Boys.mp4\n"
+ "3-05 How I Met Everyone Else.mp4\n"
+ "3-06 I'm Not That Guy.mp4\n"
+ "3-07 Dowisetrepla.mp4\n"
+ "3-08 Spoiler Alert.mp4\n"
+ "3-09 Slapsgiving.mp4\n"
+ "3-10 The Yips.mp4\n"
+ "3-11 The Platinum Rule.mp4\n"
+ "3-12 No Tomorrow.mp4\n"
+ "3-13 Ten Sessions.mp4\n"
+ "3-14 The Bracket.mp4\n"
+ "3-15 The Chain of Screaming.mp4\n"
+ "3-16 Sandcastles in the Sand.mp4\n"
+ "3-17 The Goat.mp4\n"
+ "3-18 Rebound Bro.mp4";
final String subst = "How I Met Your Mother S$1E$2 $3";
final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | 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