希望我能在这里找到帮助,因为我真的对正则表达式一无所知。我正在尝试使用从此示例开始的monaco 编辑器创建日志文件查看器,但我的日志字符串可以是多行的,我想使用不同的日期格式。所以假设我有一个这样的日志字符串:
[2017-02-03 22:07:56] [info] [Memory] After GC, total memory:737mb, used: 268mb, reclaimed: 293
[2017-02-03 22:10:15] [info] [Memory] After GC, total memory:705mb, used: 247mb, reclaimed: 141
[2017-02-03 22:10:25] [info] [Memory] After GC, total memory:705mb, used: 258mb, reclaimed: 21
[2017-02-03 22:14:34] [warn] [Evaluator] org.mozilla.javascript.EcmaError: Cannot convert null to an object.
Caused by error in Business Rule: 'GlobalHideGlobalUsersFromNonAdmins' at line 5
2:
3: var encodedQueryString = 'sys_domain!=global';
4:
==> 5: var imp = gs.getImpersonatingUserName().toString();
6: if(imp.length > 0) {
7: encodedQueryString = encodedQueryString + '^ORuser_name=' + imp;
8: }
[2017-02-03 22:14:34] [warn] [Evaluator] org.mozilla.javascript.EcmaError: Cannot convert null to an object.
Caused by error in Business Rule: 'GlobalHideGlobalUsersFromNonAdmins' at line 1
==> 1: (function executeRule(current, previous /*null when async*/) {
2:
3: var encodedQueryString = 'sys_domain!=global';
4:
这目前与我的日期格式不匹配,如果有回车符,它只会匹配日志消息的第一行,它与下一个日志消息不匹配。这里有任何可以帮助我的RegexGuru吗?:)
monaco.languages.setMonarchTokensProvider('log', {
tokenizer: {
root: [
[/\[error.*/, "custom-error"],
[/\[warn.*/, "custom-warn"],
[/\[info.*/, "custom-info"],
[/\[debug.*/, "custom-debug"],
[/\[[a-zA-Z 0-9:]+\]/, "custom-date"],
]
}
});
更新: 所以这是我想出的解决方案。显然我仍然无法匹配 [DATE] 字符串之间的多行。所以现在我将匹配例如 [error] 作为解决方法。也许有人可以把我推向正确的方向......
monaco.languages.setMonarchTokensProvider('log', {
tokenizer: {
root: [
[/\[error\]/, "custom-error"],
[/\[warn\]/, "custom-warn"],
[/\[info\]/, "custom-info"],
[/\[debug\]/, "custom-debug"],
[/^\[\d{4}[./-]\d{2}[./-]\d{2} \d{2}[./:]\d{2}[./:]\d{2}]/, "custom-date"],
]
}
});