1

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))

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":

  1. Extract first value within the first round brackets e.g PROD
  2. Extract the value between the first closing square bracket and second opening round bracket e.g. 10.10.100.100
  3. Extract the value within the second round brackets e.g EFA-B-3
  4. Extract the value within the second square brackets e.g. Brocade FC-Switch
  5. Extract the value between the second closing square bracket and the third opening round bracket e.g. Sensor: Power Supply #1
  6. Extract the value given within the third round brackets e.g. SNMP Custom Table
  7. Extract the value between the third closing round bracket and the fourth opening round bracket e.g. Down
  8. Extract the value within the fourth round brackets e.g. No Such Name (SNMP error # 2)

Using the webpage https://scriptular.com/ I tried to achieve my goal. So far I managed to build the regex

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

which gives me my first match (rule 1). Somehow I fail to declare the regex to look between the brackets. What am I missing?

2
  • 2
    let [_, system, ip, location, device, sensor, sensorArt, sensorState, errorMsg] = s.match(/\(([^()]+)\)]\s*(.*?)\s*\(([^()]*)\)\s*\[([^\][]*)]\s*(.*?)\s*\(([^()]+)\)\s*(.*?)\s*\((.*)\)/)? Commented Aug 9, 2019 at 11:46
  • @WiktorStribiżew You are a beast, its working great. Thanks for the example, I try to learn from it!! Commented Aug 9, 2019 at 12:08

1 Answer 1

2

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.

Details

  • \( - a ( char
  • ([^()]+) - Group 1: 1 or more chars other than ( and )
  • \)]\s* - )] and 0+ whitespaces
  • (.*?) - Group 2: any 0+ chars other than line break chars, as few as possible
  • \s*\( - 0+ whitespaces, (
  • ([^()]*) - Group 3: 1 or more chars other than ( and )
  • \)\s*\[ - ), 0+ whitespaces, [
  • ([^\][]*) - Group 4: 1 or more chars other than [ and ]
  • ]\s* - ] and 0+ whitespaces
  • (.*?) - Group 5: any 0+ chars other than line break chars, as few as possible
  • \s*\( - 0+ whitespaces, (
  • ([^()]+) - Group 6: 1 or more chars other than ( and )
  • \)\s* - ) and 0+ whitespaces
  • (.*?) - Group 7: any 0+ chars other than line break chars, as few as possible
  • \s*\( - 0+ whitespaces and (
  • (.*) - Group 8: any 0+ chars other than line break chars, as many as possible
  • \) - ) char.

ES6+ code snippet:

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:

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.