Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Regex script logic to parse

dvelloriy
Kilo Sage

Hi Team,

I have below script which i am using to parse host name and group name from a column in staging table.

This is working fine in some cases where alert tags have defined position (0 for host names and 2 for support group).

There are many other alerts where the positions get swapped. So i need to convert below script to use regex logic to identify Hostname and Support group values.

Please advise.

 

var str = source.tags;
//'Hostname:NAP01-OND02,Vitality:V0,Support Team:Real Time Applications,Application Name:EDS';

var arr = str.split(',');

var ci = arr[0].split(':')[1];
var suppG = arr[2].split(':')[1];

 

 

6 REPLIES 6

Brad Bowman
Kilo Patron
Kilo Patron

I don't know if regex is necessary.  It looks like you've shown an actual example string where your code is working.  What are other possibilities - a simple re-arrangement, but one of the comma-separated elements will ALWAYS starts with 'Hostname:' and one will ALWAYS starts with 'Support Team:' and in both cases you want the value from the colon to the next comma?

Hi Brad, yes please see the below alert tag. These are all comma separated and i need to extract values from Host name: and Support Team.

 

[Azure]Project Name:AS9 Upgrade,Criticality:Platinum,[Azure]Application:AS9,[Azure]CostCenter:1000,Application Name:AS9,[Azure]Support_Team:AM - Asset Suite Services,Vitality:V2,Hostname:caa-nmpoo19,[Azure]Project Number:LMC009,[Azure]Environment:PROD,Support Team:Asset Services

Something like this might be a better approach:

var str = source.tags;
var ci = '';
var suppG = '';
var arr = str.split(',');
for (i = 0; i < arr.length; i++) { 
	if (arr[i].indexOf('Hostname:') == 0) {
		ci = arr[i].split(':')[1];
	}
	if (arr[i].indexOf('Support') == 0) {
		suppG = arr[i].split(':')[1];
	}
}

 

debendudas
Mega Sage
Mega Sage

Hi @dvelloriy ,

You can use this regex to get the Hostname from this string: /(?<=Hostname:)[^,]+/ 


This regex will fetch "NAP01-OND02" from the string "Hostname:NAP01-OND02,Vitality:V0,Support Team:Real Time Applications,Application Name:EDS"

 

If this solution helps you then, mark it as accepted solution ‌‌✔️ and give thumbs up 👍