【问题标题】:Karate: match with parametrized regex空手道:与参数化正则表达式匹配
【发布时间】:2019-02-19 22:43:26
【问题描述】:

我没有找到正确的方法来编写包含变量的正则表达式匹配:

* 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}"' }

有办法做到这一点吗?

谢谢, 洛伦佐

【问题讨论】:

    标签: karate


    【解决方案1】:

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

    但无论如何这是解决方案,请参阅: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(_)' }
    

    【讨论】:

    • 我无法精确匹配。我的testmatch 是一个服务响应,我不知道哪一年(或一天,或小时,等等)。感谢您的回复,这解决了我的问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 2017-05-09
    • 2016-09-12
    相关资源
    最近更新 更多