如何在SOAPUI中使用groovy从字符串中提取数字id

如何在SOAPUI中使用groovy从字符串中提取数字id,groovy,soapui,Groovy,Soapui,其中一个服务返回的字段值如下所示,我想在SOAP UI中使用Groovy从下面的字符串中提取数字“2734427” [[https%3a%2f%2fish.is.a.sample.link%2fproduct-data-v1%2f/jobs/2734427] 我已经使用了下面的代码行-这是可行的,但它看起来有点黑客。我想知道是否有人可以建议一个更好的选择 //Get the value of the Job Id Link def gtm2joblink = "[[https%3a%2f%2f

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

[[https%3a%2f%2fish.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')
}

听起来像是正则表达式的工作。如果作业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')
}

非常感谢@craigcaulfield非常感谢@craigcaulfield