openldap 日志的正则表达式

Regex for openldap logs

我正在尝试构建一个正则表达式以将 openldap 日志划分到不同的正则表达式组

日志:

620da112 conn=2871219 op=1 SRCH attr=extensionAttribute6
620da112 conn=2871220 fd=60 ACCEPT from IP=10.17.28.159:35708 (IP=0.0.0.0:19000)
620da112 conn=2871219 op=1 SEARCH RESULT tag=101 err=0 nentries=1 text=
620da112 conn=2871219 op=2 UNBIND
620da112 conn=2871219 fd=52 closed
620da112 conn=2871220 fd=60 TLS established tls_ssf=256 ssf=256
620da112 conn=2871220 op=0 BIND dn="cn=admin,dc=ClinicalPortal" method=128

我需要创建一个正则表达式,我需要将每个正则表达式分成一个组,以便我可以将其分配给一个变量,然后在操作后将剩余部分分配给(UNBIND,关闭,TLS 建立,BIND ... ) 到不同的变量(例如:tag=101 err=0 nentries=1 text= 到 diff 变量) 到目前为止,我已经尝试了以下正则表达式,但它不适用于所有行,甚至它也没有将最后一个值(例如:tag=101 err=0 nentries=1 text= diff 变量)作为单个团体 表达式:

function printMatch(line){

  const matchings = myRe.exec(line);
  console.log("Line --START");
  if(matchings !=null){
    for(var i=0;i<matchings.length;i++){
    console.log("Group "+i+":"+matchings[i]);
    }  
  } else {
    console.log("Line "+line+" do not match the REGEX");
  }
  console.log("Line --END");

}

const myRe = /^([A-Za-z[0-9]{8})\s([=A-Za-z[0-9]*)\s([=A-Za-z[0-9]*)\s([A-Z\s{1}]*)/g;
printMatch('620da112 conn=2871219 op=1 SRCH attr=extensionAttribute6');
printMatch('620da112 conn=2871220 fd=60 ACCEPT from IP=10.17.28.159:35708 (IP=0.0.0.0:19000)');
printMatch('620da112 conn=2871219 op=1 SEARCH RESULT tag=101 err=0 nentries=1 text=');
printMatch('620da112 conn=2871219 op=2 UNBIND');
printMatch('620da112 conn=2871220 fd=60 TLS established tls_ssf=256 ssf=256');
printMatch('620da112 conn=2871220 op=0 BIND dn="cn=admin,dc=ClinicalPortal" method=128');

"Is the OP looking for a tokenizing similar to this?.. /^(?<id>[a-zA-Z0-9]{8})\s+(?<conn>\w+=\S+)\s+(?<op>\w+=\S+)\s+(?<cmd>.*?)\s*(?<rest>\w+=.*)?$/gm – Peter Seliger

"Works like a charm!!! Could you please answer this one instead of a comment would like to accept it as a answer." – Pathfinder

const multilineSample = `620da112 conn=2871219 op=1 SRCH attr=extensionAttribute6
620da112 conn=2871220 fd=60 ACCEPT from IP=10.17.28.159:35708 (IP=0.0.0.0:19000)
620da112 conn=2871219 op=1 SEARCH RESULT tag=101 err=0 nentries=1 text=
620da112 conn=2871219 op=2 UNBIND
620da112 conn=2871219 fd=52 closed
620da112 conn=2871220 fd=60 TLS established tls_ssf=256 ssf=256
620da112 conn=2871220 op=0 BIND dn="cn=admin,dc=ClinicalPortal" method=128`;

const regXNamedTokens =
  // see ... [https://regex101.com/r/k9mUPP/1]
  /^(?<id>[a-zA-Z0-9]{8})\s+(?<conn>\w+=\S+)\s+(?<op>\w+=\S+)\s+(?<cmd>.*?)\s*(?<rest>\w+=.*)?$/gm;

console.log(
  'all match results ...',
  Array.from(
    multilineSample.matchAll(regXNamedTokens)
  )
);
console.log(
  'all results each with its mapped named capture group ...',
  Array.from(
    multilineSample.matchAll(regXNamedTokens)
  )
  .map(({ groups }) => groups)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }