空手道:与参数化正则表达式匹配 [英] Karate: match with parametrized regex

查看:76
本文介绍了空手道:与参数化正则表达式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到用正则表达式共同包含变量来编写匹配的正确方法:

I didn't find the right way to write a match with regexp cointaining a variable:

* def getYear =
  """
  function() {
    var SimpleDateFormat = Java.type('java.text.SimpleDateFormat');
    var sdf = new SimpleDateFormat('yyyy');
    var date = new java.util.Date();
    return sdf.format(date);
  } 
  """
* def currentYear = getYear()
* def testmatch = { gencode: '#("000" + currentYear + "0000012345")' }
* match testmatch == { gencode: '#regex "[0-9]{3}" + currentYear + "[0-9]{10}"' }

有办法做到吗?

谢谢, 洛伦佐(Lorenzo)

Thanks, Lorenzo

推荐答案

首先,通常在进行这样的匹配时,不需要正则表达式,因为您也可以进行完全匹配.

First, normally when you are doing matches like this, regex is un-necessary, because you might as well do an exact match.

但是无论如何,这是解决方案,请参考: https://github.com/intuit/karate#self-validation-expressions

But anyway this is the solution, refer: https://github.com/intuit/karate#self-validation-expressions

* def isValidYear = function(x){ return new RegExp("[0-9]{3}" + currentYear + "[0-9]{10}").test(x) }
* assert isValidYear('00020190000012345')
* match testmatch == { gencode: '#? isValidYear(_)' }

这篇关于空手道:与参数化正则表达式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆