gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:01:08 25 4
gpt4 key购买 nike

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

当我执行特定命令时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

}
})

;}

从中我如何获取电子邮件ID testuser@gmail.com使用正则表达式可以吗?

最佳答案

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

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
> # '>'

关于regex - 使用正则表达式查找并获取特定单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24282657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com