Last active
May 26, 2022 17:28
-
-
Save drakantas/ec2c524b95a688b1618d7cc810d490c4 to your computer and use it in GitHub Desktop.
Get Discord invite links from a message using regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import re | |
DISCORD_INVITE = r'discord(?:\.com|app\.com|\.gg)[\/invite\/]?(?:[a-zA-Z0-9\-]{2,32})' | |
messages = ( | |
"Hey, join my guild and get awesome prizes. https://discord.gg/0cDvIgU2voWn4BaD", | |
"Yo. Don't join that dude's guild, these guilds are even more awesome https://discord.gg/discord-api " | |
"https://discord.gg/discord-developers", | |
"There's no invites in this message." | |
) | |
def main(): | |
for message in messages: | |
print(message + '\nINVITES FOUND:', get_invites(message), '\n') | |
# Returns a tuple of invite links if there's any, or None in case there is not. | |
# No type annotations because I want this to run anywhere. | |
def get_invites(message): | |
regex = re.compile(DISCORD_INVITE) | |
invites = regex.findall(message) | |
return invites or None | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"Your" regex wouldn't solve @GrJT's problem either.