javascript regex to map into a different group

Viewed 61

I'm trying to create a regex for the below text

[2021-11-15 23:43:41.867] INFO    [Mule-util]nz.co.ha.mule.common.logging.CustomMessageLogger [[MuleRuntime].uber.02: [sample-logging-app].sample-logging-appFlow.CPU_LITE @4ffd3e60]: event:dd3aa370-466d-11ec-83a1-0ab55c0b0cf4 ||transactionID=null|txnState=start|apiDomain=system|apiLayer=system|customMessage=Im%20here%20at%202021-11-15%2023:43:41.866|direction=incoming|messageName=sample-loggin-app|messageType=sample-loggin-app|name=main|payloadIn=false||

And i have got 5 groups out for this one, but the fourth group should be divided into 2 so i should be having 6 groups in total as below

Group-1: [2021-11-15 23:43:41.867]

Group-2: INFO

Group-3: [Mule-util]abc.co.common.logging.CustomMessageLogger

Group-4: [[MuleRuntime].uber.02: [sample-logging-app].sample-logging-appFlow.CPU_LITE @4ffd3e60]: event:

Group-5: dd3aa370-466d-11ec-83a1-0ab55c0b0cf4

Group-6: ||transactionID=null|txnState=start|apiDomain=system|apiLayer=system|customMessage=Im%20here%20at%202021-11-15%2023:43:41.866|direction=incoming|messageName=sample-loggin-app|messageType=sample-loggin-app|name=main|payloadIn=false||

NOTE: the above groups are the expected division, so currently Group-4 is holding the group-5 value as well. Can anyone let me know how can i divide the group into two, Regex Link can be found here

The reason why i wanted to have is i'm trying to use a sema-text logagent where it assigns the values of each group to a variable. I can't modify the existing functionality to read part of the payload from the above group as it is being currently used by other systems. So the only way i can do is through regex where it divides it into groups and assign to some fields provided.

My code:

var myString = "[2021-11-15 23:43:41.867] INFO    [Mule-util]nz.co.ha.mule.common.logging.CustomMessageLogger [[MuleRuntime].uber.02: [sample-logging-app].sample-logging-appFlow.CPU_LITE @4ffd3e60]: event:dd3aa370-466d-11ec-83a1-0ab55c0b0cf4 ||transactionID=null|txnState=start|apiDomain=system|apiLayer=system|customMessage=Im%20here%20at%202021-11-15%2023:43:41.866|direction=incoming|messageName=sample-loggin-app|messageType=sample-loggin-app|name=main|payloadIn=false||";
var myRegexpStr = /^(\[[0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}[.][0-9]{3}\])\s([A-Z]*)\s*([a-zA-z\[\]\.0-9:\-]*)\s([0-9a-zA-Z@\[\].\s-_:]*)?([\s|\S]+)/g;
var myRegexp = new RegExp(myRegexpStr);
var match = myRegexp.exec(myString);
console.log(match[1]); // [2021-11-15 23:43:41.867]
console.log(match[2]); // INFO
console.log(match[3]); // [Mule-util]nz.co.ha.mule.common.logging.CustomMessageLogger
console.log(match[4]); // [[MuleRuntime].uber.02: [sample-logging-app].sample-logging-appFlow.CPU_LITE @4ffd3e60]: event:dd3aa370-466d-11ec-83a1-0ab55c0b0cf4
console.log(match[5]); // ||transactionID=null|txnState=start|apiDomain=system|apiLayer=system|customMessage=Im%20here%20at%202021-11-15%2023:43:41.866|direction=incoming|messageName=sample-loggin-app|messageType=sample-loggin-app|name=main|payloadIn=false||

1 Answers

There are a lot of ways to achieve that. A possible solution will look like

^(\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}])\s+([A-Z]*)\s+([a-zA-Z0-9[\].:-]*)\s+([\w@[\].\s:-]*:)([a-fA-F0-9]{4}(?:[a-fA-F0-9]{4}-){4}[a-fA-F0-9]{12})\s+([\s\S]*)$

See the regex demo.

The main part is ([\w@[\].\s:-]*:)([a-fA-F0-9]{4}(?:[a-fA-F0-9]{4}-){4}[a-fA-F0-9]{12}):

  • ([\w@[\].\s:-]*:) - Group 4: zero or more word, @, [, ], ., :, - and whitespace chars and then a : char
  • ([a-fA-F0-9]{4}(?:[a-fA-F0-9]{4}-){4}[a-fA-F0-9]{12}) - Group 5: a UUID pattern.
Related