Skip to content

Instantly share code, notes, and snippets.

@JSeam2
Created April 23, 2019 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JSeam2/5af5efd737e39b8f63f1ce31d10fd43f to your computer and use it in GitHub Desktop.
Save JSeam2/5af5efd737e39b8f63f1ce31d10fd43f to your computer and use it in GitHub Desktop.
extract username from twitter url
import re
strings = [
"http://www.twitter.com/#!/donttrythis",
"http://twitter.com/KimKardashian",
"http://www.twitter.com/#!/KourtneuyKardash/following",
"http://twitter.com/#!/jasonterry31/lists/memberships",
]
username_pattern = re.compile("(https://|http://)?(www\.)?twitter\.com/(#!/)?(\w+)*")
for string_tomatch in strings:
result = username_pattern.match(string_tomatch)
print(result.group(4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment