简体   繁体   English

java email提取正则表达式?

[英]java email extraction regular expression?

I would like a regular expression that will extract email addresses from a String (using Java regular expressions). 我想要一个正则表达式,它将从String中提取电子邮件地址(使用Java正则表达式)。

That really works. 这确实有效。

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/

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,})";

Install this regex tester plugin into eclipse, and you'd have whale of a time testing regex 将这个正则表达式测试程序插件安装到eclipse中,你就会有一段时间测试正则表达式的鲸鱼
http://brosinski.com/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. 但是当您将正则表达式转换为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#regex中的组函数来查找这些部分的值。

([_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 例如,使用上面的正则表达式,以下字符串

abc.efg@asdf.cde

yields 产量

start=0, end=16
Group(0) = abc.efg@asdf.cde
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. 特别是当您找到以前从未考虑过的新电子邮件模式时。

a little late but ok. 有点晚了但还可以。

Here is what i use. 这是我用的。 Just paste it in the console of FireBug and run it. 只需将其粘贴到FireBug的控制台中即可运行。 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. 在网页上查找“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);

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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM