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

时间:2018-05-20 01:21:31

标签: groovy soapui

其中一项服务是返回一个类似下面这个值的字段,我想提取数字' 2734427'在SOAP UI中使用Groovy从下面的字符串

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

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

//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

1 个答案:

答案 0 :(得分:2)

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