import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "https?:\\/\\/(?:www\\.)?instagram\\.com(?:\\/[^\\/]+)?\\/(?:p|tv|reel)\\/([^\\/?#&]+)";
final String string = "https://instagram.com/p/aye83DjauH/?foo=bar#abc\n"
+ "https://www.instagram.com/reel/Chunk8-jurw/\n"
+ "https://www.instagram.com/p/BQ0eAlwhDrw/\n"
+ "https://www.instagram.com/tv/BkfuX9UB-eK/\n"
+ "https://instagram.com/p/-Cmh1cukG2/\n"
+ "http://instagram.com/p/9o6LshA7zy/embed/\n"
+ "https://www.instagram.com/tv/aye83DjauH/\n"
+ "https://www.instagram.com/reel/CDUMkliABpa/\n"
+ "https://www.instagram.com/marvelskies.fc/reel/CWqAgUZgCku/";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
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