使用正则表达式查找并获取特定单词

时间:2014-06-18 10:02:35

标签: regex node.js shell

我试图从数据返回中找到并获取特定单词

当我执行一个临时命令console.log(stdout)打印数据

commit e6c4236951634c67218172d742
Author:ttpn <testuser@gmail.com>
Date:   Mon Jun 9 10:18:04 2014 -0700

    Fix it dersk reset

..........
...........

我的代码

var getemail = function (callback) {
  var command = "git show ec6c4236951634c67218172d742";
  exec(command, function (error, stdout, stderr) {
    if (error !== null) {
      console.log(error)
      callback(error, null);
    } else {
console.log(stdout) // prints the above data

    }
  })

}

从此我如何获得email id testuser@gmail.com 使用正则表达式有可能吗?

1 个答案:

答案 0 :(得分:2)

此正则表达式将捕获您想要的数据到第1组和第2组(在demo上,查看右侧窗格中的捕获组):

commit (\S+)[\r\n]*Author:[^<]*<([^>]*)>

以下是如何在JS中使用它:

var regex = /commit (\S+)[\r\n]*Author:[^<]*<([^>]*)>/;
var match = regex.exec(string);
if (match != null) {
    theid = match[1];
    theemail = match[2];
}

解释正则表达式

commit                   # 'commit '
(                        # group and capture to \1:
  \S+                    #   non-whitespace (all but \n, \r, \t, \f,
                         #   and " ") (1 or more times (matching the
                         #   most amount possible))
)                        # end of \1
[\r\n]*                  # any character of: '\r' (carriage return),
                         # '\n' (newline) (0 or more times (matching
                         # the most amount possible))
Author:                  # 'Author:'
[^<]*                    # any character except: '<' (0 or more times
                         # (matching the most amount possible))
<                        # '<'
(                        # group and capture to \2:
  [^>]*                  #   any character except: '>' (0 or more
                         #   times (matching the most amount
                         #   possible))
)                        # end of \2
>                        # '>'