用于从数据中获取电子邮件地址的正则表达式

4

我是对正则表达式不熟悉的新手。我有以下数据,我想使用正则表达式获取唯一的电子邮件地址。如何实现?

 commit 01
 emailid: Tests <tests@gmail.com>
 Date:   Wed Jun 18 12:55:55 2014 +0530

 details

 commit 02
 emailid: user <user@gmail.com>
 Date:   Wed Jun 18 12:55:55 2014 +0530

  location
 commit 03
 emailid: Tests <tests@gmail.com>
 Date:   Wed Jun 18 12:55:55 2014 +0530

    france24
 commit 04
 emailid: developer <developer@gmail.com>
 Date:   Wed Jun 18 12:55:55 2014 +0530

    seloger

通过使用正则表达式,我如何检索tests@gmail.com,user@gmail.com,developer@gmail.com?

1个回答

5

使用这个正则表达式:

emailid: [^<]*<([^>]*)
  • emailid: 匹配该字符串文字
  • [^<]*< 匹配任何不是<的字符,然后匹配<
  • ([^>]*) 将所有不是>的字符捕获到第1组中。这就是您的电子邮件ID。

正则表达式演示中,查看右窗格中的组捕获。这就是我们要找的内容。

获取唯一的电子邮件ID

对于每个匹配项,我们检查电子邮件ID是否已经在我们的唯一电子邮件ID数组中。请参见此JS演示的输出.

var uniqueids = [];
var string = 'blah emailid: Tests <tests@gmail.com>  emailid: user <user@gmail.com> emailid: Tests <tests@gmail.com> emailid: developer <developer@gmail.com>'
var regex = /emailid: [^<]*<([^>]*)/g;
var thematch = regex.exec(string);
while (thematch != null) {
    // print the emailid, or do whatever you want with it
    if(uniqueids.indexOf(thematch[1]) <0) {
        uniqueids.push(thematch[1]);
        document.write(thematch[1],"<br />");    
    }
    thematch = regex.exec(string);
}

让我们在聊天中继续这个讨论。点击此处进入聊天室 - zx81

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接