import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(.*) ((youtube.com) | (youtu.be) | (www.youtube.com) ) (.*)";
final String string = "http://youtu.be/cCnrX1w5luM\n"
+ "www.youtube.com/cCnrX1w5luM \n"
+ "youtu.be/cCnrX1w5luM\n"
+ "https://www.youtube.com/watch?v=nzj7Wg4DAbs\n"
+ "http://www.youtube.com/watch?v=nzj7Wg4DAbs\n"
+ "youtube.com/watch?v=nzj7Wg4DAbs\n"
+ "http://www.youtube.com/watch?v=-wtIMTCHWuI\n"
+ "http://www.youtube.com/v/-wtIMTCHWuI?version=3&autohide=1\n"
+ "http://youtu.be/-wtIMTCHWuI\n"
+ "http://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3D-wtIMTCHWuI&format=json\n"
+ "Hello https://www.youtube.com/watch?v=nzj7Wg4DAbs world\n";
final String subst = "<iframe src=\"htpp://youtube.com$6\" allowfullscreen></iframe>";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE | Pattern.COMMENTS);
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