Tompa Tompa - 2 months ago 17
Java Question

Regex for Facebook Photo ID from a URL in Java

I’m trying to figure out a regex expression in Java to obtain the photo's id for this facebook url:

https://www.facebook.com/566162426788268/photos/a.566209603450217.1073741828.566162426788268/1214828765254961/?type=3&theater

I have come up with the solution

\w++(?=/\?)


but it does not work.

Help appreciated!

Answer

In Java you could look for "\w+(?=\/\?)" and since there is no match groups extracted you get group 0.

Example snippet:

    String photoIdPatternAsString = "\\w+(?=\\/\\?)";

    Matcher postIdMatcher = Pattern.compile(photoIdPatternAsString).matcher(postUrl);

    if (postIdMatcher.find()) {
        postId = postIdMatcher.group(0);
    } else throw new IOException();
Comments