来自电子邮件地址的域的正则表达式
任何人都可以帮助我使用正则表达式来返回电子邮件地址的末尾部分(@ 符号之后)吗?我是正则表达式的新手,但想学习如何使用它,而不是编写低效的 .Net 字符串函数!
例如,对于“[email protected]”的输入,我需要输出“example.com”。
干杯! 蒂姆
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 "[email protected]" I need an output of "example.com".
Cheers!
Tim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
对于此目的,正则表达式是相当重型的机器。只需在
@
字符处拆分包含电子邮件地址的字符串,然后取后半部分即可。 (电子邮件地址保证仅包含一个@
字符。)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 will match with the @, then capture everything up until the end of input ($)
这是一个通用的电子邮件匹配器:
请注意,它仅捕获域组;如果您使用以下内容,您还可以捕获进行
@
的部分:我不确定这是否符合 RFC 2822,但我对此表示怀疑。
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.
输入的一个简单正则表达式是:
但是,当您申请广泛且异构的域时,它可能毫无用处。
一个例子是:
但是,您可以根据需要优化该后缀域。
但对于您的后缀需求,您只需要:
@.+$
资源:
http://www.regular-expressions.info/email.html
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
哇,这里的所有答案都不太正确。
一个电子邮件地址可以有任意多个“@”,最后一个不一定是域名之前的那个:(
例如,这是一个有效的电子邮件地址:
你必须非常刻薄才能做到这一点不过
首先要解析掉最后的任何评论。
,
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
试试这个正则表达式:
Try this regular expression:
我不是专家,但至少据我所知,上面的表达式是您从电子邮件中获取域名所需要的。
问题正如所指出的那样,您不仅获取域名,还获取“@”和“.”。要访问正则表达式中的域名,您可以使用“$2”并保留“@”和“.”,您可以使用如下表达式:
http://www.regexr.com/
上面的站点是尝试正则表达式以查看其如何工作以及是否工作的好地方。
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:
http://www.regexr.com/
The site above is a good place to try regex to see how and if it works.
另一个较短的示例是
@([\w.-]+)\.
https://regex101.com/r/gmOH52/2
Another shorter example is
@([\w.-]+)\.
https://regex101.com/r/gmOH52/2