Open In App

Python – Extract domain name from Email address

Last Updated : 05 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a String Email address, extract the domain name.

Input : test_str = ‘manjeet@geeks.com’ 

Output : geeks.com 

Explanation : Domain name, geeks.com extracted. 

Input : test_str = ‘manjeet@gfg.com’ 

Output : gfg.com 

Explanation : Domain name, gfg.com extracted.

Method #1 : Using index() + slicing

In this, we harness the fact that “@” symbol is separator for domain name and local-part of Email address, so, index() is used to get its index, and is then sliced till end.

Python3




# Python3 code to demonstrate working of
# Extract domain name from Email address
# Using index() + slicing
 
# initializing strings
test_str = 'manjeet@geeksforgeeks.com'
 
# printing original string
print("The original string is : " + str(test_str))
 
# slicing domain name using slicing
res = test_str[test_str.index('@') + 1 : ]
 
# printing result
print("The extracted domain name : " + str(res))


Output

The original string is : manjeet@geeksforgeeks.com
The extracted domain name : geeksforgeeks.com

Time Complexity: O(n) -> (slicing)

Auxiliary Space: O(n)

Method #2 : Using split()

In this, we split the string on “@” and use its 1st index to get the required domain name.

Python3




# Python3 code to demonstrate working of
# Extract domain name from Email address
# Using split()
 
# initializing strings
test_str = 'manjeet@geeksforgeeks.com'
 
# printing original string
print("The original string is : " + str(test_str))
 
# using split() to get domain name
res = test_str.split('@')[1]
 
# printing result
print("The extracted domain name : " + str(res))


Output

The original string is : manjeet@geeksforgeeks.com
The extracted domain name : geeksforgeeks.com

Time Complexity: O(n) -> (split)

Auxiliary Space: O(n)

Method #3 : Using find() and slicing

Python3




# Python3 code to demonstrate working of
# Extract domain name from Email address
 
# initializing strings
test_str = 'manjeet@geeksforgeeks.com'
 
# printing original string
print("The original string is : " + str(test_str))
 
res=test_str[test_str.find('@')+1:]
 
# printing result
print("The extracted domain name : " + str(res))


Output

The original string is : manjeet@geeksforgeeks.com
The extracted domain name : geeksforgeeks.com

Time Complexity: O(n) -> (find function)

Auxiliary Space: O(n)

Method#4: Using the re module

This approach uses the re module to search for the domain name pattern in the email string. The regular expression pattern r”@([A-Za-z0-9.-]+)\.” matches a “@” symbol followed by one or more alphanumeric characters, hyphens, or dots, followed by a dot. The findall() method returns a list of all matches found in the email string, and we return the first match as it represents the domain name.

Algorithm

1. Import the re module.
2. Use re.findall() to search for the domain name pattern in the email string.
3. Return the first match found.
 

Python3




import re
 
def extract_domain(email):
    pattern = r"@([A-Za-z0-9.-]+)\."
    match = re.findall(pattern, email)
    return match[0]+'.com'
 
# Example usage:
email = "manjeet@gfg.com"
print(extract_domain(email))


Output

gfg.com

Time Complexity: O(n) where n is the length of the email string since re.findall() scans the string to find all matches.

Auxiliary Space: O(1) since only one match is stored in memory.

Method#5: Using itertools:

Algorithm:

  1. Define a regular expression pattern that matches the domain name in an email address.
  2. Use the dropwhile() method from the itertools module to drop all characters in the email address until the ‘@’ symbol is reached.
  3. Use the re.findall() method to extract the domain name from the remaining characters in the email address.
  4. Append the “.com” suffix to the extracted domain name.
  5. Return the domain name.

Python3




import itertools
import re
 
# Function to extract the domain names
def extract_domain(email):
    pattern = r"@([A-Za-z0-9.-]+)\."
    match = ''.join(list(itertools.dropwhile(lambda x: x != '@', email)))
    domain = re.findall(pattern, match)[0] + ".com"
    return domain
 
 
# Example Usage:
email = "manjeet@gfg.com"
 
# Print the original string
print("The original string is : " + str(email))
 
print("The extracted domain name : " + extract_domain(email))
 
# This code is contributed by Jyothi pinjala.


Output

The original string is : manjeet@gfg.com
The extracted domain name : gfg.com

Time Complexity: The time complexity of this function depends on the efficiency of the dropwhile() and re.findall() methods, which are O(n) in the length of the input string. Therefore, the time complexity of the function is O(n).

Space Complexity: The space complexity of the function is O(n) because the input string is transformed into a list of characters, and the domain name extracted from the regular expression is stored in a list of strings.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads