.net 来自电子邮件地址的域的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1487789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regular Expression for domain from email address
提问by TimS
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!
任何人都可以帮助我使用正则表达式,该表达式将在@ 符号之后返回电子邮件地址的结尾部分吗?我是 regex 的新手,但想学习如何使用它而不是编写低效的 .Net 字符串函数!
E.g. for an input of "[email protected]" I need an output of "example.com".
例如,对于“[email protected]”的输入,我需要“example.com”的输出。
Cheers! Tim
干杯! 蒂姆
回答by Stephan202
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.)
为此,正则表达式是一种非常繁重的机器。只需在字符处拆分包含电子邮件地址的@字符串,然后取下一半。(一个电子邮件地址保证只包含一个@字符。)
回答by Xetius
@(.*)$
@(.*)$
This will match with the @, then capture everything up until the end of input ($)
这将与 @ 匹配,然后捕获所有内容,直到输入结束 ($)
回答by Paul Lammertsma
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.
我不确定这是否符合 RFC 2822,但我对此表示怀疑。
回答by Neil McGuigan
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:
例如,这是一个有效的电子邮件地址:
[email protected](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);
回答by Andre Pastore
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:
但是对于您的后缀需求,您只需要:
@.+$
@.+$
回答by Hardik Sondagar
[email protected];
// 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()
- 函数文档:preg_match()
回答by rajat rastogi
Try this regular expression:
试试这个正则表达式:
(?<=([\w-\.]@))((?:[\w]+\.)+)([a-zA-Z]{2,4})
回答by Mikael
(@)(\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:
问题在于您不仅抓住了域,还抓住了“@”和“.”。要在正则表达式中访问域名,请使用“$2”并保留“@”和“.”,您可以使用如下表达式:
newdomain
The site above is a good place to try regex to see how and if it works.
上面的站点是尝试正则表达式以查看其工作方式和是否有效的好地方。

