简体   繁体   English

RegEx解析日志文件中的一行

[英]RegEx to parse a line from a log file

I have a jenkins log file that spits the following output: 我有一个jenkins日志文件,它会输出以下输出:

angularjs@1_4_7-ie8 found in path(s): public/components/angularjs-ie8-build/dist/angular.min.js 在路径中找到angularjs @ 1_4_7-ie8:public / components / angularjs-ie8-build / dist / angular.min.js

[INFO] Registered manifest into PaaS: https://deploy-apixyz.com/swdeploy/v2/manifests/demonodeserver/versions/1.0.0_20180628165604811 [INFO]已注册清单到PaaS: 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 您的构建指标已记录为ID为demonodeserver-06-29T00:07:42.845Z和manifest_id demonodeserver-1.0.0_20180628165604811

I am interested in the maniest_id part demonodeserver-1.0.0_20180628165604811 我对maniest_id部分demonodeserver-1.0.0_20180628165604811

I am looking to see if i can write a regex that can parse this. 我正在寻找我是否可以编写可以解析此的正则表达式。

Tried various methods but failed. 尝试了各种方法,但是失败了。 Can someone enlighten me? 有人可以启发我吗?

str.match(\\[demonodeserver-\\] (.*)) but this doesn't return valid result. str.match(\\[demonodeserver-\\] (.*))但这不会返回有效结果。

In order to extract demonodeserver-1.0.0_20180628165604811 from your logs based on your needs, you have several options: 为了根据需要从日志中提取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]}`); 

To create/test regexes for JavaScript, check out regex101 , but make sure you choose the JavaScript Flavor regex. 要为JavaScript创建/测试正则表达式,请检出regex101 ,但请确保选择了JavaScript Flavor正则表达式。

A regex is created with / , not \\ . 使用/而不是\\创建正则表达式。 There's also no space in your log output (after demonodeserver-1.0.0), and to get the id I'd also specify which line exactly you're interested in. The following should do the job 您的日志输出中也没有空间(在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); 

If you're interested in the manifest_id part, match on that instead of expecting the actual id to have a constant prefix (you're not always going to be using a demo node server, right?): 如果您对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]); 

The regex itself: 正则表达式本身:

/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 returns an array with the entire match at index 0 and any capture groups after that. String.match返回一个数组,该数组的整个匹配项都位于索引0处,之后是任何捕获组。 We only care about our single group, so we grab the string at index 1. 我们只关心单个组,因此我们在索引1处获取字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM