Is there a way to remove elements up to a certain element in a Python string?

Viewed 31

This might be a strange question but let me elaborate. I have a list of domains I need to go through and get their parent domain. Ex: from configuration.apple.com and help.apple.com I want to just keep apple.com. Is there a way to do this with regex? I have hundreds of domains to manipulate so this would make life infinitely easier.

I can't use a static index because of the varied nature of the subdomains. Any suggestions/help is appreciated. The logic I'm trying to use is basically to iterate through each string and when the first '.' is found, delete all previous characters and the '.' but I don't know how to make that work. Thanks in advance.

1 Answers

You don't actually need to use regex here. You can use .find(".") on a string to get the first location of a "." character, and then index the string from the character after that.

domains = ["help.apple.com", "info.microsoft.com", "configuration.apple.com"]

parents = []

for domain in domains:
    parents.append(domain[domain.find(".") + 1:])    

print(parents)
Related