Regex payload string into 6 separate payloads

Hi,

I’m really struggling with this one. I’m terrible at regex, and to be honest I’m not sure if that’s the best route either.

Please could someone help me convert the following string (below) ...

IPH[192][168][222][252] IPR[192][168][222][001] IPN[255][255][255][000] IPHW[7A][CB][E2][60][00][9A] IPP[23] IPDP[Y]

...so I have 6 separate payloads to process

Device IP,
Default Gateway,
Subnet Mask,
MAC Address,
Port,
DHCP Y/N,

Well for start - ignoring regex, you you use a change node and change all ][ to a period. you will have
IPH[192.168.222.252] IPR[192.168.222.001] IPN[255.255.255.000] IPHW[7A.CB.E2.60.00.9A] IPP[23] IPDP[Y]
then if you use a split node and split it using a blank as the 'split using', you will have

IPH[192.168.222.252]
IPR[192.168.222.001]
IPN[255.255.255.000]
IPHW[7A.CB.E2.60.00.9A]
IPP[23]
IPDP[Y]

and you should be able to process each of those messages from there.

1 Like

Use a function node...



var temp = msg.payload; // `IPH[192][168][222][252] IPR[192][168][222][001] IPN[255][255][255][000] IPHW[7A][CB][E2][60][00][9A] IPP[23] IPDP[Y]`

temp = temp.trim();
temp = temp.replace(/]\[/g, '.');//replace ][ with .
temp = temp.replace(/]/g, ',');//replace remaining ] with ,
temp = temp.replace(/\[/g, ':');//replace remaining [ with :
var spl = temp.split(','); //split by : to get an array of "item:value"
var result = {};//declare object where to put results

spl.forEach(function(item){
  if(item != ""){
    let pair = item.split(":");
    let itemName = pair[0].trim();
    let itemValue = pair[1].trim();
    result[itemName] = itemValue;
  }
})
msg.payload = result;
return msg;

click me for proof