Karate 空手道:与参数化正则表达式匹配

Karate 空手道:与参数化正则表达式匹配,karate,Karate,我没有找到使用包含变量的regexp编写匹配的正确方法: * 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); }

我没有找到使用包含变量的regexp编写匹配的正确方法:

* 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

首先,通常当你进行这样的匹配时,正则表达式是不必要的,因为你也可以进行精确匹配

但无论如何,这就是解决方案,请参考:


我无法精确匹配。我的
testmatch
是一个服务响应,我不知道哪一年(或哪一天,哪一小时,等等)是当前年份。谢谢你的回复,这解决了我的问题!
* def isValidYear = function(x){ return new RegExp("[0-9]{3}" + currentYear + "[0-9]{10}").test(x) }
* assert isValidYear('00020190000012345')
* match testmatch == { gencode: '#? isValidYear(_)' }