Skip to content

Instantly share code, notes, and snippets.

@pernydev
Last active February 2, 2023 07:44
Show Gist options
  • Select an option

  • Save pernydev/d009e2189151cdb5338a0085d46b49c7 to your computer and use it in GitHub Desktop.

Select an option

Save pernydev/d009e2189151cdb5338a0085d46b49c7 to your computer and use it in GitHub Desktop.
Link filter for OpenAI Discord (In Python)
import re
def get_domains_from_string(string_with_urls):
whitelist = ["openai.com", "stackoverflow.com"]
urls = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', string_with_urls)
domains = [re.search("(?P<domain>[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,})", url).group("domain") for url in urls]
filtered_domains = [domain for domain in domains if domain in whitelist]
return filtered_domains
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment