import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?<artist>[\\w ]+)\\/(?<album>[\\w ]+)\\/(( - )|(\\d+)|\\k<artist>|\\k<album>|[_])*(?<name>.+)(?<ext>\\.(flac|mp3))$";
final String string = "Infected Mushroom/\n"
+ "Infected Mushroom/Vicious Delicious\n"
+ "Infected Mushroom/Vicious Delicious/01 - Infected Mushroom - Becoming Insane.flac\n"
+ "Infected Mushroom/Vicious Delicious/02 - Infected Mushroom - Artillery.flac\n"
+ "Infected Mushroom/Vicious Delicious/03 - Infected Mushroom - Vicious Delicious.flac\n"
+ "Infected Mushroom/Vicious Delicious/04 - Infected Mushroom - Heavyweight.flac\n"
+ "Infected Mushroom/Vicious Delicious/05 - Infected Mushroom - Suliman.flac\n"
+ "Infected Mushroom/Vicious Delicious/06 - Infected Mushroom - Forgive Me.flac\n"
+ "Infected Mushroom/Vicious Delicious/07 - Infected Mushroom - Special Place.flac\n"
+ "Infected Mushroom/Vicious Delicious/08 - Infected Mushroom - In Front Of Me.flac\n"
+ "Infected Mushroom/Vicious Delicious/09 - Infected Mushroom - Eat It Raw.flach Mushroom - Before.flac\n"
+ "Infected Mushroom/Army of Mushrooms\n"
+ "Infected Mushroom/Army of Mushrooms/01 - Never Mind.flac\n"
+ "Infected Mushroom/Army of Mushrooms/02 - Nothing to Say.flac\n"
+ "Infected Mushroom/Army of Mushrooms/03 - Send Me an Angel.flac\n"
+ "Infected Mushroom/Army of Mushrooms/04 - U R So Fucked.flac\n"
+ "Infected Mushroom/Army of Mushrooms/05 - The Rat.flac\n"
+ "Infected Mushroom/Army of Mushrooms/06 - Nation of Wusses.flac\n"
+ "Infected Mushroom/Army of Mushrooms/07 - Wanted To.flac\n"
+ "Infected Mushroom/Army of Mushrooms/08 - Serve My Thirst.flac\n"
+ "Infected Mushroom/Army of Mushrooms/09 - I Shine.flac\n"
+ "Infected Mushroom/Army of Mushrooms/10 - Drum n' Bassa.flac\n"
+ "Infected Mushroom/Army of Mushrooms/11 - The Pretender.flac\n"
+ "Infected Mushroom/Army of Mushrooms/12 - The Messenger 2012.flac\n"
+ "Infected Mushroom/Converting Vegetarians II\n"
+ "Infected Mushroom/Converting Vegetarians II/01 - She Zoremet.flac\n"
+ "Infected Mushroom/Converting Vegetarians II/02 - Yamakas in Space.flac\n"
+ "Infected Mushroom/Converting Vegetarians II/03 - Sense of Direction.flac\n"
+ "Infected Mushroom/Converting Vegetarians II/04 - Animatronica.flac\n"
+ "Infected Mushroom/Converting Vegetarians II/05 - Feelings.flac\n"
+ "Infected Mushroom/Converting Vegetarians II/06 - Pink Froid.flac\n"
+ "Infected Mushroom/Converting Vegetarians II/07 - Demons of Pain.flac\n"
+ "Infected Mushroom/Converting Vegetarians II/08 - Zoan Zound.flac\n"
+ "Infected Mushroom/Converting Vegetarians II/09 - Blue Swan 5.flac\n"
+ "Infected Mushroom/Converting Vegetarians II/10 - Fields of Grey (feat. Sasha Grey).flac\n"
+ "Infected Mushroom/Converting Vegetarians II/11 - Leopold.flac\n"
+ "Infected Mushroom/Converting Vegetarians II/12 - On the Road Again.flac\n"
+ "Infected Mushroom/Converting Vegetarians II/13 - Stuck in a Loop.flac\n"
+ "Infected Mushroom/Converting Vegetarians II/14 - Mexicali.flac\n"
+ "Infected Mushroom/Converting Vegetarians II/15 - The Surgeon.flac\n"
+ "Infected Mushroom/Legend of the Black Shawarma\n"
+ "Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 01 - Poquito Mas.flac\n"
+ "Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 02 - Sa'eed.flac\n"
+ "Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 03 - End of the Road.flac\n"
+ "Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 04 - Smashing the Opponent (feat. Jonathan Davis).flac\n"
+ "Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 05 - Can't Stop.flac\n"
+ "Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 06 - Herbert the Pervert.flac\n"
+ "Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 07 - Killing Time (feat. Perry Ferrell).flac\n"
+ "Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 08 - Project 100.flac\n"
+ "Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 09 - Franks.flac\n"
+ "Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 10 - Slowly.flac\n"
+ "Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 11 - The Legend of the Black Shawarma.flac\n"
+ "Infected Mushroom/Legend of the Black Shawarma/Infected Mushroom - Legend of the Black Shawarma - 12 - Riders on the Storm (Infected Mushroom Remix).flac\n"
+ "Infected Mushroom/Pink Nightmares\n"
+ "Infected Mushroom/Pink Nightmares/01_Pink_Nightmares_Album_Mix.flac\n";
final String subst = "\\g<name>\\g<ext>";
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