[英]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的初学者,因此我尝试定义一些“规则”:
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.