Search code examples
pythonregexredditpraw

Regex to identify Reddit usernames


I am making a bot with a option to not post if the username is not a certain user.

Reddit usernames can contain letters in both cases, and have numbers.

Which regex can be used to identify such a username? The format is /u/USERNAME where username can have letters of both cases and numbers, such as ExaMp13.

I have tried /u/[A-Z][a-z][0-9]


Solution

  • Valid characters for Reddit usernames are preceded by /u/ and include:

    • UPPERCASE
    • lowercase
    • Digits
    • Underscore
    • Hyphen

    This regex meets those criteria:

    /u/[A-Za-z0-9_-]+