Help with regex in Event Rule from Email subject

Dhana1
Tera Contributor

Hi,

 

We have integrated SAP Cloud Alerts with Service Now and this was implemented via email methodology.

 

We have written regex in event rule to extract Resource and Metric Name from email subject value.

 

Please find the below example of Email Subject

 

emailSubject - [Alert: Action Required] _ED5_High number of ABAP Short Dumps

 

Regex : \[Alert: Action Required\] (.*)_(.*)    - working fine

 

So the above regex is working fine for all the scenarios which is following the above email subject pattern but currently they have triggered an email with an email subject as [Alert Action Required]_ED5_High number of ABAP Short Dumps which doesn't have ':' after Alert so its failing the regex and populating unknown values in Resource and Metric Name.

 

Please help me with regex which will work for both scenarios

 

[Alert: Action Required] _ED5_High number of ABAP Short Dumps

[Alert Action Required]_ED5_High number of ABAP Short Dumps

 

1 ACCEPTED SOLUTION

Runjay Patel
Giga Sage

Hi @Dhana1 ,

 

Try with below regex

\[Alert:?\s?Action Required\]_(.*)_(.*)

 

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

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

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

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@Dhana1 

Use this

\[Alert:? Action Required\] (.*)_(.*)

Explanation:

  • \[Alert:? Action Required\]: The :? makes the colon optional.
  • (.*)_(.*): Captures the resource and metric name as before.

This regex will match both [Alert: Action Required] _ED5_High number of ABAP Short Dumps and [Alert Action Required]_ED5_High number of ABAP Short Dumps.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Runjay Patel
Giga Sage

Hi @Dhana1 ,

 

Try with below regex

\[Alert:?\s?Action Required\]_(.*)_(.*)

 

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

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

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

Omkar Mone
Mega Sage

Hello 

 

Please find the below code. This regex will be generic for all kinds of text that will come, you wont need to check anything specific, it will take the correct resource and metric name -  I have tried and tested this in my pdi - 

 

var emailSubjects = [
    "[Alert: Action Required]_ED5_High number of ABAP Short Dumps",
    "[Alert Action Required]_ED5_High number of ABAP Short Dumps",
    "[Some Other Alert]_XYZ_Another Metric",
    "RandomPrefix_ED5_High number of ABAP Short Dumps"
];

var regex = /.*_(.*?)_(.*)/;

emailSubjects.forEach(function(emailSubject) {
    var resourceName = "Unknown";
    var metricName = "Unknown";

    var match = regex.exec(emailSubject);
    if (match) {
        resourceName = match[1];
        metricName = match[2];
    }

    gs.print("Email Subject: " + emailSubject);
    gs.print("Resource Name: " + resourceName);
    gs.print("Metric Name: " + metricName);
    gs.print("-------------------------------");
});

 

Output - 

*** Script: Email Subject: [Alert: Action Required]_ED5_High number of ABAP Short Dumps
*** Script: Resource Name: ED5
*** Script: Metric Name: High number of ABAP Short Dumps
*** Script: -------------------------------
*** Script: Email Subject: [Alert Action Required]_ED5_High number of ABAP Short Dumps
*** Script: Resource Name: ED5
*** Script: Metric Name: High number of ABAP Short Dumps
*** Script: -------------------------------
*** Script: Email Subject: [Some Other Alert]_XYZ_Another Metric
*** Script: Resource Name: XYZ
*** Script: Metric Name: Another Metric
*** Script: -------------------------------
*** Script: Email Subject: RandomPrefix_ED5_High number of ABAP Short Dumps
*** Script: Resource Name: ED5
*** Script: Metric Name: High number of ABAP Short Dumps
*** Script: -------------------------------

 

Hope this helps you 🙂