java java电子邮件提取正则表达式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2250820/
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
java email extraction regular expression?
提问by EugeneP
I would like a regular expression that will extract email addresses from a String (using Java regular expressions).
我想要一个从字符串中提取电子邮件地址的正则表达式(使用 Java 正则表达式)。
That really works.
那确实有效。
回答by EugeneP
Here's the regular expression that really works. I've spent an hour surfing on the web and testing different approaches, and most of them didn't work although Google top-ranked those pages.
这是真正有效的正则表达式。我花了一个小时在网上冲浪并测试了不同的方法,尽管谷歌在这些页面上排名第一,但大多数方法都不起作用。
I want to share with you a working regular expression:
我想和你分享一个有效的正则表达式:
[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})
Here's the original link: http://www.mkyong.com/regular-expressions/how-to-validate-email-address-with-regular-expression/
这是原始链接:http: //www.mkyong.com/regular-expressions/how-to-validate-email-address-with-regular-expression/
回答by thealy
I had to add some dashes to allow for them. So a final result in Javanese:
我不得不添加一些破折号以允许它们。所以最终结果是爪哇语:
final String MAIL_REGEX = "([_A-Za-z0-9-]+)(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,})";
回答by Blessed Geek
Install this regex tester plugin into eclipse, and you'd have whale of a time testing regex
http://brosinski.com/regex/.
将此正则表达式测试器插件安装到 eclipse 中,您将花大量时间测试正则表达式
http://brosinski.com/regex/。
Points to note:
In the plugin, use only one backslash for character escape. But when you transcribe the regex into a Java/C# string you would have to double them as you would be performing two escapes, first escaping the backslash from Java/C# string mechanism, and then second for the actual regex character escape mechanism.
注意点:
在插件中,字符转义只使用一个反斜杠。但是,当您将正则表达式转录为 Java/C# 字符串时,您必须将它们加倍,因为您将执行两次转义,首先从 Java/C# 字符串机制中转义反斜杠,然后是实际的正则表达式字符转义机制。
Surround the sections of the regex whose text you wish to capture with round brackets/ellipses. Then, you could use the group functions in Java or C# regex to find out the values of those sections.
用圆括号/椭圆包围您希望捕获的正则表达式部分。然后,您可以使用 Java 或 C# 正则表达式中的组函数来找出这些部分的值。
([_A-Za-z0-9-]+)(\.[_A-Za-z0-9-]+)@([A-Za-z0-9]+)(\.[A-Za-z0-9]+)
([_A-Za-z0-9-]+)(\.[_A-Za-z0-9-]+) @([A-Za-z0-9]+)(\.[A-Za-z0 -9]+)
For example, using the above regex, the following string
例如,使用上面的正则表达式,下面的字符串
[email protected]
yields
产量
start=0, end=16
Group(0) = [email protected]
Group(1) = abc
Group(2) = .efg
Group(3) = asdf
Group(4) = .cde
Group 0 is always the capture of whole string matched.
组 0 总是捕获匹配的整个字符串。
If you do not enclose any section with ellipses, you would only be able to detect a match but not be able to capture the text.
如果您没有用省略号将任何部分括起来,您将只能检测到匹配项而不能捕获文本。
It might be less confusing to create a few regex than one long catch-all regex, since you could programmatically test one by one, and then decide which regexes should be consolidated. Especially when you find a new email pattern that you had never considered before.
创建几个正则表达式可能比创建一个长的包罗万象的正则表达式更容易混淆,因为您可以以编程方式一个一个地进行测试,然后决定应该合并哪些正则表达式。尤其是当您发现以前从未考虑过的新电子邮件模式时。
回答by Digital Human
a little late but ok.
有点晚,但还好。
Here is what i use. Just paste it in the console of FireBug and run it. Look on the webpage for a 'Textarea' (Most likely on the bottom of the page) That will contain a , seperated list of all email address found in A tags.
这是我使用的。只需将其粘贴到 FireBug 的控制台中并运行它。在网页上查找“Textarea”(最有可能在页面底部),它将包含在 A 标签中找到的所有电子邮件地址的单独列表。
var jquery = document.createElement('script');
jquery.setAttribute('src', 'http://code.jquery.com/jquery-1.10.1.min.js');
document.body.appendChild(jquery);
var list = document.createElement('textarea');
list.setAttribute('emaillist');
document.body.appendChild(list);
var lijst = "";
$("#emaillist").val("");
$("a").each(function(idx,el){
var mail = $(el).filter('[href*="@"]').attr("href");
if(mail){
lijst += mail.replace("mailto:", "")+",";
}
});
$("#emaillist").val(lijst);
回答by Duy Pham
The Java 's build-in email address pattern (Patterns.EMAIL_ADDRESS) works perfectly:
Java 的内置电子邮件地址模式 ( Patterns.EMAIL_ADDRESS) 完美运行:
public static List<String> getEmails(@NonNull String input) {
List<String> emails = new ArrayList<>();
Matcher matcher = Patterns.EMAIL_ADDRESS.matcher(input);
while (matcher.find()) {
int matchStart = matcher.start(0);
int matchEnd = matcher.end(0);
emails.add(input.substring(matchStart, matchEnd));
}
return emails;
}

