Global Switch (/g) in RegEx parsing strategy in Pattern Designer

knightofmirrors
Kilo Contributor

Has anyone been able to successfully get the Global (/g) switch to work successfully in Pattern Designer for a RegEx parsing strategy?  

If I write a pattern that successfully is iterated using the Global switch and it tests correctly in a tool like Regexr, that same pattern won't match on subsequent iterations when I try with /g in the Pattern Designer.

I am trying to build a tabular variable and would like an additional row for each match of the pattern

Thanks!

Sean

1 ACCEPTED SOLUTION

Asi Garty
ServiceNow Employee
ServiceNow Employee

Hi,

Unfortunately the global switch is not supported, and the expression matchs each line separately.
The good news are that it is on the road map as a future enhancement.

If you are populating a table it will iterate through the entire text and add new table row for each successful (non global) match.

in order to globally match the entire text, you should write a pattern step with an eval statement and implement a simple java script that will run regexp match with any required flags.

Hope this helps.

If it does please mark if helpful and share your results.

Asi

View solution in original post

8 REPLIES 8

Asi Garty
ServiceNow Employee
ServiceNow Employee

Hey Johnny,

I can suggest 2 approaches this can be done:

1. Simple way: just use an existing string variable inside your eval statement and add delimiter (i.e. ";" or "|") between each match result.
Then it is easy to convert it into a table in another step using delimited text parsing operation.

2. Create a table variable inside your eval statement and then use ctx.setAttribute() function to create it as a context variable.
The exact variable type is dependent in your eval language, and I don't remember the exact type.
If the first approach is not feasible, drop me a message and I'll find out for you. 

Asi

1) if I had a string of these type comma seperated, how would I make a table out of them? I want a table of hosts and ports. "tcp://yaddahost:1829,tcp://yaddahost2:1822"

 

2)a javascript example of creating a table structure from scratch would be amazing. I believe the structure is an ArrayList of HashMaps. They are quite fragile and they break easily, from what I'm seeing. I get a .toString() value of the ArrayList if I make the slightest booboo in handling it.

//this almost works - What I'm not able to do yet,
//is add a row (HashMap) to the table (ArrayList)
//this code causes the pattern step to spin and spin
//any tips on how to adjust this would be appreciated

var rtrn = '';
var jms_conn = ${jms_connections};

//the mistake is here - don't call .size() within the
//for loop that you add a row within - derp derp
for (var i=0; i < ${jms_connections}.size(); i++){
   var hmap = jms_conn.get(i);
   var mapped = hmap.get('values')+'';
   if (mapped.indexOf(',') > 1){
      var parts = mapped.split(',');
      var myMap = hmap.clone();
      myMap.put('new_header', parts[1]);
      jms_conn.add(myMap);
      myMap = hmap.clone();
      myMap.put('new_header', parts[0]);
      jms_conn.add(myMap);
   } else {
      hmap.put('new_header', mapped);
   }
   jms_conn.remove(i);
}

//rtrn = jms_conn;

CTX.setAttribute("tmp2", jms_conn);
rtrn = jms_conn.size();

//got it - this actually works

var jms_conn = ${jms_connections};
var orig_size = ${jms_connections}.size();

for (var i=0; i < orig_size; i++){
   var hmap = jms_conn.get(i);
   var mapped = hmap.get('values')+'';
   if (mapped.indexOf(',') > 1){
      var parts = mapped.split(',');
      //put this part back
      hmap.put('new_header', parts[1]+'');
      //make a new row for the other part
      var myMap = new Packages.java.util.LinkedHashMap(hmap);
      myMap.put('new_header', parts[0]+'');
      jms_conn.add(myMap);
      
   } else {
      //provide the orig value in this new column
      hmap.put('new_header', mapped);
   }
}

CTX.setAttribute("tmp2", jms_conn);