来自电子邮件地址的域的正则表达式

发布于 2024-08-06 05:05:43 字数 277 浏览 5 评论 0原文

任何人都可以帮助我使用正则表达式来返回电子邮件地址的末尾部分(@ 符号之后)吗?我是正则表达式的新手,但想学习如何使用它,而不是编写低效的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

落花随流水 2024-08-13 05:05:43

对于此目的,正则表达式是相当重型的机器。只需在 @ 字符处拆分包含电子邮件地址的字符串,然后取后半部分即可。 (电子邮件地址保证仅包含一个 @ 字符。)

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.)

谁的年少不轻狂 2024-08-13 05:05:43

@(.*)$

这将与 @ 匹配,然后捕获所有内容,直到输入结束 ($)

@(.*)$

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

南街女流氓 2024-08-13 05:05:43

这是一个通用的电子邮件匹配器:

[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])

请注意,它仅捕获域组;如果您使用以下内容,您还可以捕获进行 @ 的部分:

([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])

我不确定这是否符合 RFC 2822,但我对此表示怀疑。

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.

时光与爱终年不遇 2024-08-13 05:05:43

输入的一个简单正则表达式是:

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

但是,当您申请广泛且异构的域时,它可能毫无用处。

一个例子是:

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

但是,您可以根据需要优化该后缀域。

但对于您的后缀需求,您只需要:

@.+$

资源:
http://www.regular-expressions.info/email.html

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

清风夜微凉 2024-08-13 05:05:43

哇,这里的所有答案都不太正确。

一个电子邮件地址可以有任意多个“@”,最后一个不一定是域名之前的那个:(

例如,这是一个有效的电子邮件地址:

[email protected](i'm a comment (with an @))

你必须非常刻薄才能做到这一点不过

首先要解析掉最后的任何评论。

int atIndex = emailAddress.LastIndexOf("@");
String domainPart = emailAddress.Substring(atIndex + 1);

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);
萌逼全场 2024-08-13 05:05:43
[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"
  • 函数文档:preg_match()
[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()
您的好友蓝忘机已上羡 2024-08-13 05:05:43

试试这个正则表达式:

(?<=([\w-\.]@))((?:[\w]+\.)+)([a-zA-Z]{2,4})

Try this regular expression:

(?<=([\w-\.]@))((?:[\w]+\.)+)([a-zA-Z]{2,4})
岁月如刀 2024-08-13 05:05:43
(@)(\w+\-|\w+)+(\.)

我不是专家,但至少据我所知,上面的表达式是您从电子邮件中获取域名所需要的。

问题正如所指出的那样,您不仅获取域名,还获取“@”和“.”。要访问正则表达式中的域名,您可以使用“$2”并保留“@”和“.”,您可以使用如下表达式:

$1newdomain$3

http://www.regexr.com/

上面的站点是尝试正则表达式以查看其如何工作以及是否工作的好地方。

(@)(\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.

残月升风 2024-08-13 05:05:43

另一个较短的示例是 @([\w.-]+)\.

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文