Create query instead of RegX

GeroM
Kilo Explorer

I got the following RegX and need help how to use or transfer it for an encoded query to get all matching CI names (server names with class "Windows Server" from table "cmdb_ci_server").

[i,I][m,M][c,C][i,I,t,T,p,P][e,E]0[0,d,D,f,F,a,A,p,P] [0,a,A,b,B][0-9][a,A][0-9][0-9]

 

Example for a result (server name): imcie0000a00

 

2 REPLIES 2

Brad Bowman
Kilo Patron

I don't know that you can use regex in a GlideRecord query, so you may have you loop through all of the class, pushing the matching results to an array.

var matchArr = [];
var svr = new GlideRecord('cmdb_ci_server');
svr.addQuery('sys_class_name', 'cmdb_ci_win_server');
svr.query();
while (svr.next()) {
	var regex = new RegExp("[i,I][m,M][c,C][i,I,t,T,p,P][e,E]0[0,d,D,f,F,a,A,p,P] [0,a,A,b,B][0-9][a,A][0-9][0-9]");
	if (regex.test(svr.name)) {
		matchArr.push(svr.name);
	}
}

Hello @Brad Bowman  ,

 

Regex is working server side script :

 

var regex = /^[iI][mM][cC][iItTpP][eE]0[0dDfFaApP][0aAbB][0-9][aA][0-9][0-9]$/;

var gr = new GlideRecord('cmdb_ci_server');
gr.addQuery('sys_class_name', 'cmdb_ci_win_server');
gr.query();

while (gr.next()) {
     var name = gr.getValue('name');
    //added this to check regex work or not : var name = "imcie0000a00"
    gs.print(name)

    if (name && name.match(regex)) {
        gs.print('Matched CI: ' + name);
    }
    else{
        gs.print("not matched")
    }
}
 
 
adityahubli_0-1770054752039.png

 

 

adityahubli_1-1770054781400.png