import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^\\[((?!(?:ti|ar|al|au|lr|length|by|offset|re|tool|ve|#|\\d{1,2}):.+\\]).*)\\](\\r?\\n)(\\[\\d{1,2}:\\d{1,2}\\.\\d{2,3}\\])(.*)$";
final String string = "[ti:Hey You]\n"
+ "[ar:Pink Floyd]\n"
+ "[al:The Wall (2011 Remaster)]\n"
+ "[au:Roger Waters]\n"
+ "[lr:Roger Waters]\n"
+ "[length:04:40]\n"
+ "[by: Casual Tea]\n"
+ "[offset:+300]\n"
+ "[re: Musicbee]\n"
+ "[tool: Mp3tag]\n"
+ "[ve: 1.2.3]\n"
+ "[#: A comment]\n\n"
+ "[Acoustic Guitar Intro]\n\n"
+ "[Verse 1: David Gilmour]\n"
+ "[00:35.485]Hey you\n"
+ "[00:38.184]Out there in the cold,\n"
+ "[00:39.828]Getting lonely, getting old,\n"
+ "[00:41.848]Can you feel me?\n"
+ "[00:46.137]Hey you\n"
+ "[00:48.659]Standing in the aisles\n"
+ "[00:50.250]With itchy feet and fading smiles,\n"
+ "[00:52.611]Can you feel me?\n"
+ "[00:57.924]Hey, you, don't help them to bury the light\n"
+ "[01:08.393]Don't give in without a fight\n"
+ "[01:13.227]\n"
+ "[Verse 2: David Gilmour]\n"
+ "[01:20.282]Hey, you\n"
+ "[01:23.035]Out there on your own,\n"
+ "[01:24.566]Sitting naked by the phone,\n"
+ "[01:26.685]Would you touch me?\n"
+ "[01:30.958]Hey, you\n"
+ "[01:33.071]With your ear against the wall,\n"
+ "[01:35.171]Waiting for someone to call out,\n"
+ "[01:37.348]Would you touch me?\n"
+ "[01:42.711]Hey you, would you help me to carry the stone?\n"
+ "[01:52.884]Open your heart, I'm coming home\n"
+ "[01:58.076]\n"
+ "[Guitar Solo]\n\n"
+ "[Bridge: Roger Waters]\n"
+ "[02:57.282]But it was only fantasy\n"
+ "[03:04.137]The wall was too high, as you can see\n"
+ "[03:11.617]No matter how he tried, he could not break free\n"
+ "[03:19.019]And the worms ate into his brain\n"
+ "[03:22.914]\n"
+ "[Verse 3: Roger Waters]\n"
+ "[03:54.974]Hey, you\n"
+ "[03:57.745]Out there on the road,\n"
+ "[03:59.261]Always doing what you're told,\n"
+ "[04:01.459]Can you help me?\n"
+ "[04:05.745]Hey, you\n"
+ "[04:08.135]Out there beyond the wall,\n"
+ "[04:09.899]Breaking bottles in the hall,\n"
+ "[04:12.108]Can you help me?\n"
+ "[04:17.481]Hey, you, don't tell me there's no hope at all\n"
+ "[04:27.360]Together we stand, divided we fall\n\n"
+ "[Outro: Roger Waters]\n"
+ "[04:32.742](We fall)\n"
+ "[04:34.133](We fall)\n"
+ "[04:35.313](We fall)\n"
+ "[04:36.593](We fall)\n"
+ "[04:37.885](We fall)\n"
+ "[04:39.178](We fall)\n"
+ "[04:40.433]";
final String subst = "$3($1)$2$3$4";
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