How to extract specific text from string

Amit41
Tera Expert

Hello All,

I am trying to extract 'Severity 3' text from below string however unable to pull it:

START_REQUEST
SUMMARY:Test incident
DESCRIPTION:Ticket created for Test User

Test ticket


/DESCRIPTION
REF:TICKET#234566
CATEGORY:
ISSUE_TYPE:
SERVICE:
ASSIGNMENTGROUP:
SEVERITY:Severity 3
USER_NAME:P
MAIL:test@test.com
PHONE:-
USER_LOCATION:-
NUMBER_OF_IMPACTED_USERS:Single user
ENVIRONMENT:PRODUCTION
END_REQUEST

I am trying below expression but it's returning 'NULL' where I passed above email text to temp1 variable:

var regex = new SNC.Regex('/Sev\\d{7}/im');

var val = regex.match(temp1);
gs.info(">>>>>>" + val);

Please help.

Thank you,

Amit 

4 REPLIES 4

Filipe Cruz
Kilo Sage
Kilo Sage

Hello Amit,

try like this:

var start = temp.indexOf("SEVERITY:");
var sev = temp.substring(start+9, start+19);

gs.print(sev);

Tested this and is printing "Severity 3".

hope this helps!

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Best Regards,

Filipe Cruz

Abish
Mega Guru
Mega Guru

Hi Amit

I assume your asking how to pull "Severity 3 " from the string

var temp1=" /DESCRIPTIONREF:TICKET#234566CATEGORY:ISSUE_TYPE:SERVICE:ASSIGNMENTGROUP:SEVERITY:Severity 3USER_NAME:PMAIL:test@test.comPHONE:-USER_LOCATION:-NUMBER_OF_IMPACTED_USERS:Single userENVIRONMENT:PRODUCTIONEND_REQUEST";

var regex = new SNC.Regex('/Severity 3/');

var val = regex.match(temp1);
gs.info(">>>>>>" + val);

This returns the string when the string "Severity 3" is present. If not it returns null.

Hope this helps!

Regards

Abi R

 

Thank you Abi.

This works but I need to write 4 different regex to capture severities. Is it possible to check for multiple severities in single Regex.

Regards,

Amit

Hi @Amit41 

Try this

// Get the text value from wherever it's stored (e.g. a variable or form field)
var textValue = "START_REQUEST\nSUMMARY:Test incident\nDESCRIPTION:Ticket created for Test User\n\nTest ticket\n\n\n/DESCRIPTION\nREF:TICKET#234566\nCATEGORY:\nISSUE_TYPE:\nSERVICE:\nASSIGNMENTGROUP:\nSEVERITY:Severity 3\nUSER_NAME:P\nMAIL:test@test.com\nPHONE:\nUSER_LOCATION:\nNUMBER_OF_IMPACTED_USERS:Single user\nENVIRONMENT:PRODUCTION\nEND_REQUEST:";

// Extract the Severity value using a regular expression
var severityMatch = textValue.match(/SEVERITY:(.*)/i);
var severity = '';
if (severityMatch) {
severity = severityMatch[1].trim();
}

// Use the Severity value in some way (e.g. set a form field value)
g_form.setValue('field_name', severity);