- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2023 09:53 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2023 10:24 PM
Hello @Sunil25 ,
Please try the below expression:
/[^\r\n.]+\.[^\r\n.]+\.(.*)/
Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2023 10:16 PM
Hi @Sunil25 ,
Can you share the script which you tried?
Thanks and Regards,
Rahul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2023 10:24 PM
Hello @Sunil25 ,
Please try the below expression:
/[^\r\n.]+\.[^\r\n.]+\.(.*)/
Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2023 10:56 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2023 10:35 PM
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