import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?<=(?:\\[embed\\])|(?:\\[embed\\]\\s)|(?:))(https?:\\/\\/)(:?www\\.)?(:?youtube\\.com\\/watch|youtu\\.be\\/)([\\w\\?=\\&]+)(?=(?:\\[\\/embed\\])|(?:\\s\\[\\/embed\\])|(?:))";
final String string = "bla https://www.youtube.com/watch?v=Vpg9yizPP_g\n"
+ "http://www.youtube.com/watch?v=Vpg9yizPP_g bla\n"
+ "[embed]https://www.youtube.com/watch?v=Vpg9yizPP_g[/embed] bla\n"
+ "[embed] https://www.youtube.com/watch?v=Vpg9yizPP_g [/embed]\n"
+ "[embed]http://www.youtube.com/watch?v=Vpg9yizPP_g[/embed]\n"
+ "[embed] http://www.youtube.com/watch?v=Vpg9yizPP_g [/embed]\n"
+ "bla https://youtube.com/watch?v=Vpg9yizPP_g bla\n"
+ "http://youtube.com/watch?v=Vpg9yizPP_g\n"
+ "[embed]https://youtube.com/watch?v=Vpg9yizPP_g[/embed]bla\n"
+ "bla[embed] https://youtube.com/watch?v=Vpg9yizPP_g [/embed]\n"
+ "[embed]http://youtube.com/watch?v=Vpg9yizPP_g[/embed]\n"
+ "[embed] http://youtube.com/watch?v=Vpg9yizPP_g [/embed]\n"
+ "https://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g\n"
+ "http://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g\n"
+ "[embed]https://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g[/embed]\n"
+ "[embed] https://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g [/embed]\n"
+ "[embed]http://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g[/embed]\n"
+ "bla[embed] http://www.youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g [/embed]bla\n"
+ "https://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g bla\n"
+ "bla http://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g\n"
+ "[embed]https://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g[/embed]\n"
+ "[embed] https://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g [/embed]\n"
+ "[embed]http://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g[/embed]\n"
+ "[embed] http://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g [/embed]\n"
+ "https://youtu.be/Vpg9yizPP_g\n"
+ "http://youtu.be/Vpg9yizPP_g bla\n"
+ "[embed]https://youtu.be/Vpg9yizPP_g[/embed]\n"
+ "[embed] https://youtu.be/Vpg9yizPP_g [/embed]\n"
+ "[embed]http://youtu.be/Vpg9yizPP_g[/embed]\n"
+ "[embed] http://youtu.be/Vpg9yizPP_g [/embed]\n"
+ "<a href src=\"http://youtu.be/Vpg9yizPP_g\">vid</a>\n"
+ "<a href src=\"http://youtube.com/watch?feature=player_embedded&v=Vpg9yizPP_g\">vid</a>\n"
+ "<a href src=\"https://youtube.com/watch?v=Vpg9yizPP_g\">vid</a>";
final String subst = "\\1\\2\\3\\4";
final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
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