正则表达式从数据中获取电子邮件ID

时间:2021-04-30 15:16:01

Am new to regular experssion i have following data from this i want to get the unique email id.How it is possible using regular expresison

我是常规表现的新手,我有以下数据,我希望获得唯一的电子邮件ID。如何使用常规表达式

 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

From this using regular experssion how can i retirve tests@gmail.com,user@gmail.com,developer@gmail.com

从这个使用常规experssion我如何retirve测试@ gmail.com,user @ gmail.com,developer @ gmail.com

1 个解决方案

#1


5  

With this regex:

有了这个正则表达式:

emailid: [^<]*<([^>]*)
  • emailid: matches that string literal
  • emailid:匹配该字符串文字
  • [^<]*< matches any characters that are not a <, then matches <
  • [^ <] * <匹配任何不是<的字符,然后匹配<< li>
  • ([^>]*) captures all characters that are not a > to Group 1. This is your emailid.
  • ([^>] *)捕获所有不属于> 1的字符。这是您的emailid。

In the regex demo, look at the Group captures in the right pane. This is what we are looking for.

在正则表达式演示中,查看右侧窗格中的“组捕获”。这就是我们正在寻找的。

Getting Unique emailids

获取唯一的电子邮件

For each match, we check if the emailid is already in our array of unique email ids. See the output of this JS demo.

对于每个匹配,我们检查emailid是否已经在我们的唯一电子邮件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);
}

#1


5  

With this regex:

有了这个正则表达式:

emailid: [^<]*<([^>]*)
  • emailid: matches that string literal
  • emailid:匹配该字符串文字
  • [^<]*< matches any characters that are not a <, then matches <
  • [^ <] * <匹配任何不是<的字符,然后匹配<< li>
  • ([^>]*) captures all characters that are not a > to Group 1. This is your emailid.
  • ([^>] *)捕获所有不属于> 1的字符。这是您的emailid。

In the regex demo, look at the Group captures in the right pane. This is what we are looking for.

在正则表达式演示中,查看右侧窗格中的“组捕获”。这就是我们正在寻找的。

Getting Unique emailids

获取唯一的电子邮件

For each match, we check if the emailid is already in our array of unique email ids. See the output of this JS demo.

对于每个匹配,我们检查emailid是否已经在我们的唯一电子邮件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);
}