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.

Event Rule Regex Parsing

Jordan35
Mega Contributor

I am trying to parse an email with regex in an event rule and keep running into issues. Have tested over a dozen different ways to parse but everything keeps returning extra content or returns <<unknown>>.

Current regex is just trying to get Service but I ultimately need a few different things.

.*Service:\s(.*).*

What I am trying to parse looks like this -

-----------------------------------------------

email body text that changes and is useless. Contains some times 10:00 pm etc. and other info that is unimportant.

Service: important company info

Data Center: US East

Identity Domain: important423525

Then there is a whole bunch of unimportant stuff down here that we don't need. 

 

--------------------------------------------

What I am trying to get is an expression that has the data after Service, Data Center and Identity Domain. Any suggestions would be greatly appreciated.

4 REPLIES 4

OlaN
Giga Sage
Giga Sage

Hi,

How about parsing the data line by line, and when handling each line, look for the specific texts, and store the needed information in a variable to use after all the processing (all text is read) is done.

Otherwise you will need to create 3 separate regular expressions, or a very complicated one, to match these texts.

 

Jordan35
Mega Contributor

The plan is to create 3 separate expressions. I have been trying to just get the first thing to work here first. Have not had luck without specifying exactly what we are expecting to see in the match string. 

SumanthDosapati
Mega Sage
Mega Sage

Hi,

May be you can use split('\n') to break each line into an array element and go through each line and get required data.

 

Regards,
Sumanth

Muhammad Khan
Mega Sage

Hi Jordan,

 

I have created the following background script to demonstrate regex usage as per your requirement. Although, it might need some fine tunings.

var str = "-----------------------------------------------'\n'\nemail body text that changes and is useless. Contains some times 10:00 pm etc. and other info that is unimportant.'\n'\nService: important company info'\n'\nData Center: US East'\n'\nIdentity Domain: important423525'\n'\nThen there is a whole bunch of unimportant stuff down here that we don't need.'\n'\n'\n'\n --------------------------------------------"

gs.print(str.match(/Service: [a-z\s]*/ig));
gs.print(str.match(/Data Center: [a-z\s]*/ig));
gs.print(str.match(/Identity Domain: [a-z0-9\s]*/ig));

Output

find_real_file.png

 

Hopefully, this will help you fine tune your regex.