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

问题描述 投票:0回答:1

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

[[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
groovy soapui
1个回答
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')
}
© www.soinside.com 2019 - 2024. All rights reserved.