简体   繁体   English

正则表达式 - JavaScript / Blockly

[英]REGEX - JavaScript / Blockly

I have two (or more) different export from our system in raw data.我从我们的系统中导出了两个(或更多)不同的原始数据。 I would like to separete it by using regex without using IF foreach case.我想通过使用正则表达式而不使用 IF foreach 来分离它。

Bellow are two examples:下面是两个例子:

  1. <14>Apr 29 10:00:00 nimble1-A NMBL: Array:nimblegroup Type:14883 Time:Fri Apr 29 10:00:00 2022#012 Id:8234 Target:nimble2-nimble1 Version:6.0.0.300-956221-opt Message:Successfully created a snapshot of the volumes associated with volume collection nimble2-nimble1 schedule test on synchronous replication partners pool-nimble2 and pool-nimble1. <14>4 月 29 日 10:00:00 nimble1-A NMBL:数组:nimblegroup 类型:14883 时间:4 月 29 日星期五 10:00:00 2022#012 ID:8234 目标:nimble2-nimble1 版本:6.0.0.300-956221- opt 消息:已成功创建与同步复制伙伴 pool-nimble2 和 pool-nimble1 上的卷收集 nimble2-nimble1 计划测试关联的卷的快照。
  2. <14>May 1 00:53:01 nimble1-A NMBL: Group:nimblegroup Type:1016 Time:Sun May 1 00:53:01 2022#012 Id:9106 Object Id:- Object: Access Type:su Client IP:Console Status:Succeeded Version:6.0.0.300-956221-opt Message:Elevating user privilege to admin <14>5 月 1 日 00:53:01 nimble1-A NMBL:组:nimblegroup 类型:1016 时间:2022 年 5 月 1 日星期日 00:53:01#012 Id:9106 Object Id:- Object:访问类型:su Client IP:控制台状态:成功版本:6.0.0.300-956221-opt 消息:正在将用户权限提升为管理员

In the first log I need to get only the Message .在第一个日志中,我只需要获取Message But in the othercase I need to get Object, Type, Client IP, Status, Message .但在其他情况下,我需要获取Object, Type, Client IP, Status, Message

I expect that I need to use ?我希望我需要使用 in regex , but I dont know how without using IF for every case.在 regex 中,但我不知道如何不对每种情况使用 IF 。

Blockly image example here.此处为块状图像示例。

Thank you, for your help.谢谢您的帮助。

If i understand you right, you have above entries as strings.如果我没理解错的话,您将以上条目作为字符串。

With one single regular expression, it will be hard to do that.使用一个正则表达式,很难做到这一点。 I personally would prefer a mix of a regular expression and split() and try to make a regular object out of it first.我个人更喜欢正则表达式和split()的混合,并尝试首先从中创建一个常规的 object。

Something like:就像是:

var entriesRaw = [
    '<14>Apr 29 10:00:00 nimble1-A NMBL: Array:nimblegroup Type:14883 Time:Fri Apr 29 10:00:00 2022#012 Id:8234 Target:nimble2-nimble1 Version:6.0.0.300-956221-opt Message:Successfully created a snapshot of the volumes associated with volume collection nimble2-nimble1 schedule test on synchronous replication partners pool-nimble2 and pool-nimble1.',
    '<14>May 1 00:53:01 nimble1-A NMBL: Group:nimblegroup Type:1016 Time:Sun May 1 00:53:01 2022#012 Id:9106 Object Id:- Object: Access Type:su Client IP:Console Status:Succeeded Version:6.0.0.300-956221-opt Message:Elevating user privilege to admin'
];

var entries = entriesRaw.map(function(entry) {
    return entry.split(/((?:Array|Client IP|Group|Id|Message|Object(?: Id)?|Status|Target|Time|Type|Version):)/g).reduce((acc, chunk, i, chunks) => {
        if(chunk.slice(-1) === ':' && i < chunks.length - 1) {
            var tmp = {};
            tmp[chunk.slice(0, -1)] = chunks[i + 1].trim();
            
            return Object.assign({}, acc, tmp);
        }
        
        return acc;
    }, {});
});

console.log(entries);

This way you have all of entries parsed into an array of objects with key-value pairs and you can access them.通过这种方式,您可以将所有条目解析为具有键值对的对象数组,并且您可以访问它们。

The trick is to do backtracking inside of the split() , because this way the split chunks are part of the resulting array.诀窍是在split()内部进行回溯,因为这样分割块就是结果数组的一部分。

The problem with your entries is, that their syntax is hard to parse, so you have to come up with each key as a literal string in the regular expression.您的条目的问题是,它们的语法很难解析,因此您必须将每个键作为正则表达式中的文字字符串。

If you need additional help, feel free to ask.如果您需要其他帮助,请随时提出。 :-) :-)

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

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