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.

Regular expression to extract digits after second dot

Sunil25
Mega Guru

Hello all,

 

I am trying to write a script to extract string after second dot with regular expression. The text is not consistent and hence I am unable to use Split(), Substring() methods.

 

Example:

Text could be "255.255.244" --> I want "244"
Text could be "10.0.4567" --> I want "4567"

Text could be "1.3.2368.23" --> I want "2368.23"

Text could be "10.2e.435" --> I want "435"

 

Please suggest how I can extract with regular expression script.

 

Thanks, 

Sunil Safare

1 ACCEPTED SOLUTION

Nayan  Dhamane
Kilo Sage

Hello @Sunil25 ,

Please try the below expression:
/[^\r\n.]+\.[^\r\n.]+\.(.*)/

If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

View solution in original post

4 REPLIES 4

Rahul Talreja
Mega Sage

Hi @Sunil25 ,
Can you share the script which you tried?

Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

Nayan  Dhamane
Kilo Sage

Hello @Sunil25 ,

Please try the below expression:
/[^\r\n.]+\.[^\r\n.]+\.(.*)/

If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

Hello @Nayan Dhamane 

 

Thanks a lot and the solution worked as a charm. This is the below script I have tried in Background script.

 

var str = '1.3.2368.23';
var regex = /[^\r\n.]+\.[^\r\n.]+\.(.*)/;
var myText = regex.exec(str);

gs.info('Required String is ->' + myText[1]);

 

OUTPUT:

Required String is ->2368.23

 

Thanks,

Sunil Safare

Samaksh Wani
Giga Sage

Hello @Sunil25 

 

 

var str = "255.255.255";
var arr= str.split('.');
var res = arr[arr.length-1];
gs.info(res);

 

 

Plz Mark my Solution as Accept and Give me thumbs up, if you find it helpful.

 

Regards,

Samaksh