Tompa Tompa - 1 year ago 74
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:

I have come up with the solution


but it does not work.

Help appreciated!

Answer Source

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 =;
    } else throw new IOException();