Search code examples
c++regexapiurlimgur

Parse imgur links for album, image and gifv IDs


Imgur links come in a lot of different formats:

http://imgur.com/alphanumeric

https://imgur.com/alphanumeric

https://i.imgur.com/alphanumeric.jpg

https://i.imgur.com/alphanumeric.gifv

http://imgur.com/gallery/alphanumeric

https://i.sstatic.net/TQSbJ.jpg (daisy chained together)

imgur.com/a/{alphanumeric}

imgur.com/gallery/{alphanumeric}

imgur.com/g/{alphanumeric}

http://imgur.com/r/SUBREDDIT/IMAGEID

https://i.sstatic.net/PsiCX.jpg

There's probably a lot more too, but I'm not a frequent Imgur user, so I don't know. What I want to do is to take these links and get the image or album ID so I can use it with their API to find whether it's a jpg, gifv, album, etc. I'm using C++, with wstrings, so maybe regex will help here? The links sometimes have http://, sometimes https:// sometimes neither. I'm not too familiar with regex (only used it once before), so I don't know if it would really help here.


Solution

  • OK, I figured it out myself. Not sure why my original question got downvoted so much. Anyway:

    wstring str;
    wregex filter{ L"[^/]*$" };
    std::wsmatch result;
    regex_search(str, result, filter);
    str = result.str();