简体   繁体   English

javascript多个正则表达式匹配

[英]javascript multiple regex matches

Given the string below 给定下面的字符串

[NeMo (PROD)] 10.10.100.100 (EFA-B-3) [Brocade FC-Switch ] Sensor: Power Supply #1 (SNMP Custom Table) Down (No Such Name (SNMP error # 2)) [NeMo(PROD)] 10.10.100.100(EFA-B-3)[Brocade FC-Switch]传感器:电源1(SNMP自定义表)关闭(没有这样的名称(SNMP错误2))

I try to get multiple matches to extract the following values: 我尝试获取多个匹配项以提取以下值:

var system = "PROD";
var ip = "10.10.100.100";
var location = "EFA-B-3";
var device = "Brocade FC-Switch";
var sensor = "Sensor: Power Supply #1";
var sensorArt = "SNMP Custom Table";
var sensorState = "Down";
var errorMsg = "No Such Name (SNMP error # 2)";

Since I am a beginner with regex I tried to define some "rules": 由于我是regex的初学者,因此我尝试定义一些“规则”:

  1. Extract first value within the first round brackets eg PROD 提取第一个圆括号内的第一个值,例如PROD
  2. Extract the value between the first closing square bracket and second opening round bracket eg 10.10.100.100 提取第一个封闭的方括号和第二个开放的圆括号之间的值,例如10.10.100.100
  3. Extract the value within the second round brackets eg EFA-B-3 提取第二个括号内的值,例如EFA-B-3
  4. Extract the value within the second square brackets eg Brocade FC-Switch 提取第二个方括号内的值,例如Brocade FC-Switch
  5. Extract the value between the second closing square bracket and the third opening round bracket eg Sensor: Power Supply #1 提取第二个方括号和第三个方括号之间的值,例如传感器:电源#1
  6. Extract the value given within the third round brackets eg SNMP Custom Table 提取第三个圆括号内给出的值,例如SNMP Custom Table
  7. Extract the value between the third closing round bracket and the fourth opening round bracket eg Down 提取第三个闭合圆括号和第四个闭合圆括号之间的值,例如Down
  8. Extract the value within the fourth round brackets eg No Such Name (SNMP error # 2) 提取第四个圆括号内的值,例如“ 否这样的名称”(SNMP错误#2)

Using the webpage https://scriptular.com/ I tried to achieve my goal. 我使用https://scriptular.com/网页来实现自己的目标。 So far I managed to build the regex 到目前为止,我设法构建了正则表达式

(?=(([^)]+))) (?=(([^)] +)))

which gives me my first match (rule 1). 这是我的第一场比赛(规则1)。 Somehow I fail to declare the regex to look between the brackets. 我以某种方式未能声明正则表达式在括号之间查找。 What am I missing? 我想念什么?

Since there is no way to define separators, the only way is to match the parts and capture them separately. 由于无法定义分隔符,因此唯一的方法是匹配零件并分别捕获它们。

/\(([^()]+)\)]\s*(.*?)\s*\(([^()]*)\)\s*\[([^\][]*)]\s*(.*?)\s*\(([^()]+)\)\s*(.*?)\s*\((.*)\)/

See the regex demo . 参见regex演示

Details 细节

  • \\( - a ( char \\( -一个(字符
  • ([^()]+) - Group 1: 1 or more chars other than ( and ) ([^()]+) -组1:除()以外的1个或更多字符
  • \\)]\\s* - )] and 0+ whitespaces \\)]\\s* - )]和0+个空格
  • (.*?) - Group 2: any 0+ chars other than line break chars, as few as possible (.*?) -第2组:除换行符以外的任何0+个字符,且尽可能少
  • \\s*\\( - 0+ whitespaces, ( \\s*\\( -0+空格, (
  • ([^()]*) - Group 3: 1 or more chars other than ( and ) ([^()]*) -第3组:比其它1个或多个字符()
  • \\)\\s*\\[ - ) , 0+ whitespaces, [ \\)\\s*\\[ - ) ,0 +个空格, [
  • ([^\\][]*) - Group 4: 1 or more chars other than [ and ] ([^\\][]*) -组4: []以外的1个或更多字符
  • ]\\s* - ] and 0+ whitespaces ]\\s* - ]和0+空格
  • (.*?) - Group 5: any 0+ chars other than line break chars, as few as possible (.*?) -第5组:除换行符以外的任何0+个字符,请尽可能少
  • \\s*\\( - 0+ whitespaces, ( \\s*\\( -0+空格, (
  • ([^()]+) - Group 6: 1 or more chars other than ( and ) ([^()]+) -组6:除()以外的1个或更多字符
  • \\)\\s* - ) and 0+ whitespaces \\)\\s* - )和0+空格
  • (.*?) - Group 7: any 0+ chars other than line break chars, as few as possible (.*?) -组7:除换行符以外的任何0+字符,请尽可能少
  • \\s*\\( - 0+ whitespaces and ( \\s*\\( -0+空格和(
  • (.*) - Group 8: any 0+ chars other than line break chars, as many as possible (.*) -第8组:比换行符字符以外的任何字符0+,尽可能
  • \\) - ) char. \\) - )字符。

ES6+ code snippet: ES6 +代码段:

 var s = "[NeMo (PROD)] 10.10.100.100 (EFA-B-3) [Brocade FC-Switch ] Sensor: Power Supply #1 (SNMP Custom Table) Down (No Such Name (SNMP error # 2))"; let [_, system, ip, location1, device, sensor, sensorArt, sensorState, errorMsg] = s.match(/\\(([^()]+)\\)]\\s*(.*?)\\s*\\(([^()]*)\\)\\s*\\[([^\\][]*)]\\s*(.*?)\\s*\\(([^()]+)\\)\\s*(.*?)\\s*\\((.*)\\)/); console.log(`System=${system}\\nIP=${ip}\\nLocation=${location1}\\nDevice=${device}\\nSensor=${sensor}\\nSensorArt=${sensorArt}\\nSensorState=${sensorState}\\nErrorMsg=${errorMsg}`); 

ES5: ES5:

 var s = "[NeMo (PROD)] 10.10.100.100 (EFA-B-3) [Brocade FC-Switch ] Sensor: Power Supply #1 (SNMP Custom Table) Down (No Such Name (SNMP error # 2))"; var system, ip, location1, device, sensor, sensorArt, sensorState, errorMsg; var rx = /\\(([^()]+)\\)]\\s*(.*?)\\s*\\(([^()]*)\\)\\s*\\[([^\\][]*)]\\s*(.*?)\\s*\\(([^()]+)\\)\\s*(.*?)\\s*\\((.*)\\)/; if (m = s.match(rx)) { system = m[1]; ip = m[2]; location1=m[3]; device=m[4]; sensor=m[5]; sensorArt=m[6]; sensorState=m[7]; errorMsg=m[8]; } console.log("System="+system+"\\nIP="+ip+"\\nLocation="+location1+"\\nDevice="+device+"\\nSensor="+sensor+"\\nSensorArt="+sensorArt+"\\nSensorState="+sensorState+"\\nErrorMsg="+errorMsg); 

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

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