RegEx解析日志文件中的一行

时间:2018-06-29 20:14:36

标签: javascript regex

我有一个jenkins日志文件,它会显示以下输出:

  在路径中找到的

angularjs @ 1_4_7-ie8:   public / components / angularjs-ie8-build / dist / angular.min.js

     

[INFO]已注册清单到PaaS:   https://deploy-apixyz.com/swdeploy/v2/manifests/demonodeserver/versions/1.0.0_20180628165604811

     

您的构建指标已记录为ID   demonodeserver-06-29T00:07:42.845Z和manifest_id   demonodeserver-1.0.0_20180628165604811

我对maniest_id部分demonodeserver-1.0.0_20180628165604811

感兴趣

我正在寻找是否可以编写可解析此内容的正则表达式。

尝试了各种方法,但失败了。有人可以启发我吗?

str.match(\[demonodeserver-\] (.*)),但这不会返回有效结果。

3 个答案:

答案 0 :(得分:2)

为了根据需要从日志中提取demonodeserver-1.0.0_20180628165604811,您有几种选择:

const log = `
angularjs@1_4_7-ie8 found in path(s): public/components/angularjs-ie8-build/dist/angular.min.js

[INFO] Registered manifest into CMPaaS: https://deploy-apixyz.com/swdeploy/v2/manifests/demonodeserver/versions/1.0.0_20180628165604811

Your build metrics have been recorded with id demonodeserver-06-29T00:07:42.845Z and manifest_id demonodeserver-1.0.0_20180628165604811
`;

// If you need the tag part only:
const patternTagOnly = /manifest_id\s+\w+-([\w:\.]+)/m;
const tag = log.match(patternTagOnly)[1];
console.log(`tag only: ${tag}`);

// If you need the name part only:
const patternNameOnly = /manifest_id\s+(\w+)-[\w:\.]+/m;
const name = log.match(patternNameOnly)[1];
console.log(`name only: ${name}`);

// If you need the name and tag part too, unseparated:
const patternFull = /manifest_id\s+(\w+-[\w:\.]+)/m;
const full = log.match(patternFull)[1];
console.log(`full: ${full}`);

// If you need the name and tag part too, but separated:
const patternFullSeparated = /manifest_id\s+(\w+)-([\w:\.]+)/m;
const parts = log.match(patternFullSeparated);
console.log(`full, separated: ${parts[1]} - ${parts[2]}`);

要创建/测试JavaScript正则表达式,请签出regex101,但请确保选择 JavaScript Flavor 正则表达式。

答案 1 :(得分:2)

使用/而不是\创建正则表达式。在日志输出中(demonodeserver-1.0.0之后)也没有空间,并且要获取ID,我还要指定您确切感兴趣的行。以下应完成此工作

const str = `angularjs@1_4_7-ie8 found in path(s):
public/components/angularjs-ie8-build/dist/angular.min.js

[INFO] Registered manifest into CMPaaS:
https://deploy-apixyz.com/swdeploy/v2/manifests/demonodeserver/versions/1.0.0_20180628165604811

Your build metrics have been recorded with id
demonodeserver-06-29T00:07:42.845Z and manifest_id
demonodeserver-1.0.0_20180628165604811`;

const regex = /demonodeserver-(\d\.?){3}_\w+/gm;

const match = str.match(regex);
console.log(match);

答案 2 :(得分:0)

如果您对manifest_id部分感兴趣,请对其进行匹配,而不是期望实际的id具有常量前缀(您并不总是要使用演示节点服务器,对吗?):

const log = `angularjs@1_4_7-ie8 found in path(s): public/components/angularjs-ie8-build/dist/angular.min.js
[INFO] Registered manifest into CMPaaS: https://deploy-apixyz.com/swdeploy/v2/manifests/demonodeserver/versions/1.0.0_20180628165604811
Your build metrics have been recorded with id demonodeserver-06-29T00:07:42.845Z and manifest_id demonodeserver-1.0.0_20180628165604811`;

console.log(log.match(/manifest_id (\S+)/)[1]);

正则表达式本身:

/manifest_id (\S+)/
/                 / regexes are delimited by /
 manifest_id        a literal "manifest_id "
             (   )  capture the stuff in here and put it in the match array
              \S    any non-whitespace character
                +   one or more times

String.match返回一个数组,该数组的整个匹配项都位于索引0处,之后是任何捕获组。我们只关心单个组,因此我们在索引1处获取字符串。