Regular Expressions 101

Community Patterns

Reddit URL Regex

1

Regular Expression
Python

r"
^http(?:s)?://(?:www\.)?(?:[\w-]+?\.)?reddit.com(/r/|/user/)?(?(1)([\w:\.]{2,21}))(/comments/)?(?(3)(\w{5,9})(?:/[\w%\\\\-]+)?)?(?(4)/(\w{3,9}))?/?(\?)?(?(6)(\S+))?(\#)?(?(8)(\S+))?$
"
gm

Description

Regex for parsing subreddit, IDs, and URL params out of a Reddit URL. Battle-hardened and tested for over three years on Reddit bots GifReversingBot, vredditshare, and switcharoohelper.

Returns the following information if it is exists in a Reddit URL

  • /r/ or /user/ depending on whether this is in a sub or a user page
  • subreddit name
  • /comments/ if this is a comments page (mostly useless, just needed to do subsequent captures)
  • Submission ID
  • Comment ID
  • ? if there are URL parameters
  • URL Parameters (you'll need to parse them through something else to get keys and values)
Submitted by pmdevita - 3 years ago (Last modified a year ago)