How to extract domain name from email address

Retrieve the domain name from an email address

This tutorial shows you on how to extract the domain name from an email address in different programming languages, such as PHP, Java, VB .NET, C#, Python, Javascript and Ruby programming language. The below sample code is useful when you need to extract the domain name to be supplied into FraudLabs Pro REST API (for email_domain field) or any other project requirements.

Extract Domain in PHP

$email = '';
$domain = substr($email, strpos($email, '@') + 1);

Extract Domain in Java

String email = "";
String domain = email .substring(email .indexOf("@") + 1);

Extract Domain in VB.NET

Dim email As String = ""
Dim startIndex As Integer = email.IndexOf("@")
Dim endIndex As Integer = email.IndexOf(".", startIndex)

Dim domain As String = email.SubString(startIndex + 1, endIndex)

Extract Domain in C#

string email = "";
int indexOfAt = email.IndexOf('@');
string domain = email.Substring(indexOfAt + 1);

Extract Domain in Python

email = ''
domain = email.split('@')[1]

Extract Domain in Javascript

const emailAddress = "";

// Extract the domain
const domain = emailAddress.split('@')[1];


Extract Domain in Ruby

email_address = ""

# Extract the domain using a regular expression
domain = email_address.match(/@(.+)$/)
puts domain[1]

Ready to start with FraudLabs Pro?

Get Micro plan for free, you can quickly explore and integrate with our fraud prevention solution in minutes.

Was this article helpful?

Related Articles