如何在 SOAP UI 中使用 groovy 从字符串中提取数字 ID

How to extract a numeric id from a string using groovy in SOAP UI

其中一项服务正在返回一个值如下所示的字段,我想在 SOAP UI [=14] 中使用 Groovy 从下面的字符串中提取数字“2734427” =]

[[https%3a%2f%2fthis.is.a.sample.link%2fproduct-data-v1%2f/jobs/2734427]]

我使用了下面的代码行 - 有效,但看起来有点老套。想知道是否有人可以提出更好的选择。

//Get the value of the Job Id Link 
def gtm2joblink = "[[https%3a%2f%2fthis.is.a.sample.link%2fproduct-data-v1%2f/jobs/2734427]]"
// split jobid full link for extracting the actual id  
def sub1 = { it.split("jobs/")[1] }
def jobidwithbrackets = sub1(gtm2joblink)
// split jobid full link for extracting the actual id  
def sub2 = { it.split("]]")[0] }
def jobid = sub2(jobidwithbracket)


log.info gtm2joblink

听起来像是正则表达式的工作。如果作业 ID 总是跟在 /jobs 之后,并且总是数字,并且最后总是有双括号 ]],那么下面将提取 ID:

import java.util.regex.Matcher 

//Get the value of the Job Id Link 
def gtm2joblink = "[[https%3a%2f%2fthis.is.a.sample.link%2fproduct-data-v1%2f/jobs/2734427]]"

Matcher regexMatcher = gtm2joblink =~ /(?ix).*\/jobs\/([0-9]*)]]/
if (regexMatcher.find()) {
    String jobId = regexMatcher.group(1);
    log.info(jobId)
} else  {
    log.info('No job ID found')
}