Untitled

 avatar
unknown
plain_text
23 days ago
2.4 kB
4
Indexable
package com.example.demo.util;

import java.util.regex.Pattern;

public class FacebookUrlValidation {
    
    private static final String FB_VIDEO_PATTERN = 
        "^https?://(?:www\\.)?facebook\\.com/" +
        "(?:" +
            // Standard video formats with username
            "(?:[^/?]+/)?videos/(?:short/|vb\\.[0-9]+/)?[0-9]+/?|" +
            // Video formats with username and variations
            "(?:[^/?]+/)?video/[0-9]+/?|" +
            "(?:[^/?]+/)?media/[0-9]+/?|" +
            // Share formats (including posts, videos, and reels)
            "share/[prvw]/[A-Za-z0-9]+/?|" +
            // Reel formats
            "(?:[^/?]+/)?reel(?:s)?/[0-9]+/?|" +
            // Watch formats
            "watch/(?:\\?v=[^/&]+|[^/?]+/?)|" +
            // Video.php format
            "video\\.php\\?v=[^/&]+|" +
            // Embedded video format
            "video/embed\\?video_id=[0-9]+|" +
            // Playlist and album formats
            "(?:[^/?]+/)?videos/playlist/[0-9]+/?|" +
            "(?:[^/?]+/)?album/[0-9]+/videos/?|" +
            // Post formats that might contain videos
            "(?:[^/?]+/)?posts/[0-9]+/?|" +
            "permalink\\.php\\?story_fbid=[0-9]+|" +
            // Additional video formats
            "(?:[^/?]+/)?videos/[^/]+/[0-9]+/?|" +
            "(?:[^/?]+/)?video/[^/]+/[0-9]+/?" +
        ")$";

    /**
     * Validates if the given URL is a valid Facebook video URL.
     * This method checks for all possible Facebook video URL formats including:
     * - Regular video URLs (with or without username)
     * - Short video URLs
     * - Video URLs with vb.ID format
     * - Share URLs (p for posts, r for reels, v for videos, w for watch)
     * - Reel URLs (all variations)
     * - Watch URLs
     * - Video.php URLs
     * - Embedded video URLs
     * - Playlist URLs
     * - Album video URLs
     * - Media URLs
     * - Post URLs (since posts can contain videos)
     * 
     * @param url The URL to validate
     * @return true if the URL is a valid Facebook video URL, false otherwise
     */
    public static boolean isValidFacebookVideoUrl(String url) {
        if (url == null || url.trim().isEmpty()) {
            return false;
        }
        return Pattern.compile(FB_VIDEO_PATTERN, Pattern.CASE_INSENSITIVE).matcher(url.trim()).matches();
    }
}
Leave a Comment