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
A simple regex for your input is:
But, it can be useless when you apply for a broad and heterogeneous domains.
An example is:
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
@(.*)$
This will match with the @, then capture everything up until the end of input ($)
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.)This is a general-purpose e-mail matcher:
Note that it only captures the domain group; if you use the following, you can capture the part proceeding the
@
also:I'm not sure if this meets RFC 2822, but I doubt it.
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:
You'd have to be pretty mean to make that your email address though.
So first, parse out any comments at the end.
Then