java 邮件提取正则表达式?

发布于 2024-08-21 10:53:36 字数 62 浏览 8 评论 0原文

我想要一个从字符串中提取电子邮件地址的正则表达式(使用 Java 正则表达式)。

这确实有效。

I would like a regular expression that will extract email addresses from a String (using Java regular expressions).

That really works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

寄意 2024-08-28 10:53:36

这是真正有效的正则表达式。
我花了一个小时在网上冲浪并测试不同的方法,
尽管谷歌将这些页面排名最高,但其中大多数都不起作用。

我想与您分享一个有效的正则表达式:

[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})

这是原始链接:
http://www.mkyong。 com/正则表达式/如何使用正则表达式验证电子邮件地址/

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/

蝶…霜飞 2024-08-28 10:53:36

我必须添加一些破折号才能允许它们。所以爪哇语的最终结果是:

final String MAIL_REGEX = "([_A-Za-z0-9-]+)(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})";

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,})";
梦途 2024-08-28 10:53:36

将这个正则表达式测试器插件安装到 eclipse 中,您将有大量时间测试正则表达式
http://brosinski.com/regex/

注意事项:
在插件中,仅使用一个反斜杠进行字符转义。但是,当您将正则表达式转录为 Java/C# 字符串时,您必须将它们加倍,因为您将执行两次转义,首先从 Java/C# 字符串机制转义反斜杠,然后第二次转义实际的正则表达式字符转义机制。

用圆括号/省略号将要捕获其文本的正则表达式部分括起来。然后,您可以使用 Java 或 C# 正则表达式中的组函数来找出这些部分的值。

([_A-Za-z0-9-]+)(\.[_A-Za-z0-9-]+)@([A-Za-z0-9]+)(\.[A- Za-z0-9]+)

例如,使用上面的正则表达式,以下字符串

[email protected]

产生的

start=0, end=16
Group(0) = [email protected]
Group(1) = abc
Group(2) = .efg
Group(3) = asdf
Group(4) = .cde

Group 0 始终捕获整个匹配的字符串。

如果您没有用省略号括起任何部分,您将只能检测到匹配项,但无法捕获文本。

创建几个正则表达式可能比创建一个长的包罗万象的正则表达式更容易混淆,因为您可以通过编程方式一一测试,然后决定应该合并哪些正则表达式。尤其是当您发现以前从未考虑过的新电子邮件模式时。

Install this regex tester plugin into eclipse, and you'd have whale of a time testing regex
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.

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.

([_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.

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.

妄想挽回 2024-08-28 10:53:36

有点晚了但是还可以。

这是我用的。只需将其粘贴到 FireBug 的控制台中并运行即可。在网页上查找“文本区域”(最有可能位于页面底部),其中将包含 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);

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.

    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);
千纸鹤带着心事 2024-08-28 10:53:36

Java 的内置电子邮件地址模式 (模式。 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;
    }

The Java 's build-in email address pattern (Patterns.EMAIL_ADDRESS) works perfectly:

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