11

Can anyone help me with a regular expression that will return the end part of an email address, after the @ symbol? I'm new to regex, but want to learn how to use it rather than writing inefficient .Net string functions!

E.g. for an input of "test@example.com" I need an output of "example.com".

Cheers! Tim

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
TimS
  • 5,922
  • 6
  • 35
  • 55

9 Answers9

15

A regular expression is quite heavy machinery for this purpose. Just split the string containing the email address at the @ character, and take the second half. (An email address is guaranteed to contain only one @ character.)

Stephan202
  • 59,965
  • 13
  • 127
  • 133
  • 5
    This is not technically true as you can have multiple @ signs if and only if the @ sign is in a quoted string http://en.wikipedia.org/wiki/Email_address#Syntax For all intents and purposes of email address verification, a single @ sign is the splitter. – Nate Noonen Aug 23 '12 at 20:12
14

@(.*)$

This will match with the @, then capture everything up until the end of input ($)

Xetius
  • 44,755
  • 24
  • 88
  • 123
  • 3
    Thanks! I'm getting "@example.com". Can I get the value without the @? – TimS Sep 28 '09 at 16:20
  • 1
    You're probably not getting the right group. The parenthesis `(`, `)` indicate a group match, so make sure you grab the result from group with index 1, not 0 (as index #0 is always reserved for the entire expression match). – Paul Lammertsma Sep 28 '09 at 17:06
  • This is the correct answer as the OP specifically asked for a regex... which incidentally is what I was looking for when I arrived here :) – Jens Ehrich Nov 11 '14 at 17:29
4

This is a general-purpose e-mail matcher:

[a-zA-Z][\w\.-]*[a-zA-Z0-9]@([a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z])

Note that it only captures the domain group; if you use the following, you can capture the part proceeding the @ also:

([a-zA-Z][\w\.-]*[a-zA-Z0-9])@([a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z])

I'm not sure if this meets RFC 2822, but I doubt it.

Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
3

Wow, all the answers here are not quite right.

An email address can have as many "@" as you want, and the last one isn't necessarily the one before the domain :(

for example, this is a valid email address:

user@example.com(i'm a comment (with an @))

You'd have to be pretty mean to make that your email address though.

So first, parse out any comments at the end.

Then

int atIndex = emailAddress.LastIndexOf("@");
String domainPart = emailAddress.Substring(atIndex + 1);
Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152
2

A simple regex for your input is:

^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$

But, it can be useless when you apply for a broad and heterogeneous domains.

An example is:

^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)$

But, you can optimize that suffix domains as you need.

But for your suffix needs, you need just:

@.+$

Resources: http://www.regular-expressions.info/email.html

Andre Pastore
  • 2,841
  • 4
  • 33
  • 44
1
$input=hardiksondagar@gmail.com;
// now you want to fetch gmail from input user PHP's inbuilt function 
preg_match('/@(.*)/', $input, $output);
echo $output[1]; // it'll print "gmail.com"
  • Documentation of function : preg_match()
Hardik Sondagar
  • 4,347
  • 3
  • 28
  • 48
  • 2
    That link has *absolutely nothing* to do with the question posted here. **Do not simply link to your website like this**, or you will find your posts being deleted as spam. I am removing the link for you. – Andrew Barber Dec 26 '12 at 11:11
  • I know it has nothing to do with the question but link contains very tough example so think It'll be useful to others...Ok I'll take care o f it next time. Thanks :-) – Hardik Sondagar Dec 26 '12 at 12:26
1

Try this regular expression:

(?<=([\w-\.]@))((?:[\w]+\.)+)([a-zA-Z]{2,4})
Spontifixus
  • 6,570
  • 9
  • 45
  • 63
0
(@)(\w+\-|\w+)+(\.)

I am no expert but the above expression is what you need to grab the domain from an e-mail, at least as far as I can tell.

The problem is as pointed out that you grab not only the domain but the "@" and ".". To access the domain name in regex you use "$2"and to preserve the "@" and ".", you could use an expression like:

$1newdomain$3

http://www.regexr.com/

The site above is a good place to try regex to see how and if it works.

Mikael
  • 1
0

Another shorter example is @([\w.-]+)\.

https://regex101.com/r/gmOH52/2

0m3r
  • 12,286
  • 15
  • 35
  • 71