fork download
  1. import re
  2.  
  3. pattern = re.compile(r"\btwitter(?:\.com/|:?(?!\s*(?:https?://|twitter\b))\s+@?)([\w.-]+)", re.IGNORECASE)
  4.  
  5. def get_username(string):
  6. m = pattern.search(string)
  7. if m:
  8. return m.group(1)
  9. return None
  10.  
  11. print(get_username("Twitter: https://t...content-available-to-author-only...r.com/foo123"))
  12. print(get_username("Twitter: twitter.com/foo123"))
  13. print(get_username("https://t...content-available-to-author-only...r.com/foo123"))
  14. print(get_username("https://t...content-available-to-author-only...r.com/foo123?blah"))
  15. print(get_username("Twitter foo123"))
  16. print(get_username("Twitter @foo123"))
  17. print(get_username("Twitter: foo123"))
  18. print(get_username("Twitter: foo123 | youtube: ..."))
Success #stdin #stdout 0.03s 9644KB
stdin
Standard input is empty
stdout
foo123
foo123
foo123
foo123
foo123
foo123
foo123
foo123