python - Stripping multiple characters from the start of a string -


i'm trying trim sub-string beginning of string based on condition:

for instance, if input domain name prefixed http, https and/or www, needs strip these , return domain.

here's have far:

if my_domain.startswith("http://"):         my_domain = my_domain[7:] elif my_domain.startswith("https://"):         my_domain = my_domain[8:]  if my_domain.startswith("www."):         my_domain = my_domain[4:]  print my_domain 

i've tried use these inbuilt functions (.startswith) instead of trying use regex.

while code above works, i'm wondering if there more efficient way combine conditions make code shorter or have multiple checks in same conditional statement?

i know regex computationally slower lot of built in methods lot easier write code wise :)

import re re.sub("http[s]*://|www\." , "", my_domain) 

edit: mentioned @dunes more correct way of answering problem is.

re.sub(r"^https?://(www\.)?" , "" , my_domain) 

old answer left reference dunes comment still has context.


Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -