使用#regex标记出现问题 [英] Trouble using #regex marker

查看:45
本文介绍了使用#regex标记出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在构造适当的#regex标记时遇到了麻烦.

I'm having trouble constructing a proper #regex marker.

我正在尝试在响应JSON中匹配几种日期格式:

I'm trying to match on a couple of date formats in my response JSON:

"created": "2017-03-23T14:16:25.854Z" "modified": "2018-06-21T05:38:37.978Z"

"created": "2017-03-23T14:16:25.854Z" "modified": "2018-06-21T05:38:37.978Z"

我尝试了以下标记:

'#regex [0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}Z' '#regex [0-9]{4}-[0-9]{2}-[0-9]{2}.[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}.' '#regex \d+-\d+-\d+.\d+:\d+:\d+.\d+.'

'#regex [0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}Z' '#regex [0-9]{4}-[0-9]{2}-[0-9]{2}.[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}.' '#regex \d+-\d+-\d+.\d+:\d+:\d+.\d+.'

所有3种形式似乎都是正确的(根据rubular.com).我也玩过转义可能会出现问题的角色.到目前为止,我唯一能上班的人是:

All 3 forms appear to be correct (according to rubular.com). I've also played around with escaping characters that might be problematic. The only one I been able to get tot work so far is:

[0-9-T:.Z]+

但这感觉有点像模式匹配.

But this feels a little "loose" of a pattern match.

基本上我正在尝试:

* def meta = { created: '#regex[[0-9]{4}-[0-9]{2}-[0-9]{2}.[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}.]', modified: '#regex[[0-9]{4}-[0-9]{2}-[0-9]{2}.[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}.]' } And match each response == """ { id: '#regex [a-z0-9]+', name: '#string', type: '<Type>', meta: #(meta), integration_id: '#uuid' } """

* def meta = { created: '#regex[[0-9]{4}-[0-9]{2}-[0-9]{2}.[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}.]', modified: '#regex[[0-9]{4}-[0-9]{2}-[0-9]{2}.[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}.]' } And match each response == """ { id: '#regex [a-z0-9]+', name: '#string', type: '<Type>', meta: #(meta), integration_id: '#uuid' } """

得到与此类似的错误:

KarateException: objects-api.feature:40 - path: $[0].meta.created, actual: '2016-11-30T20:48:16.782Z', expected: '#regex [0-9]+-[0-9]+-[0-9]+[0-9]+:[0-9]+:[0-9]+[0-9]+', reason: regex match failed

推荐答案

这里是一个建议,为什么不将日期解析为java Date对象,这将带来更多的可能性,例如能够比较2日期.

Here is a suggestion, why don't you parse the dates into java Date objects and that will open up more possibilities such as being able to compare 2 dates.

以下是很好的例子: https://stackoverflow.com/a/54133126/143475 https://stackoverflow.com/a/55938480/143475

Here are good examples: https://stackoverflow.com/a/54133126/143475 https://stackoverflow.com/a/55938480/143475

也就是说,我认为您错过了必须避免使用反斜杠-es的问题,这是Java方面的内容,并在文档中进行了提及.这样就可以了:

That said, I think you missed having to escape the backslash-es, this is a Java thing and is mentioned in the docs. So this works:

* def date = '2017-03-23T14:16:25.854Z'
* match date == '#regex \\d+-\\d+-\\d+.\\d+:\\d+:\\d+.\\d+.'
* match date == '#regex [0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}Z'

空手道JS引擎还支持RegExp JS对象,因此请查看此示例是否也为您提供一些解决方案:

The Karate JS engine also supports the RegExp JS object, so see if this example gives you some solutions as well:

https://stackoverflow.com/a/54768838/143475

编辑,这对我也适用:

* def meta = { created: '#regex \\d+-\\d+-\\d+.\\d+:\\d+:\\d+.\\d+.', modified: '#regex \\d+-\\d+-\\d+.\\d+:\\d+:\\d+.\\d+.' }
* def response = [{ meta: { created: '2017-03-23T14:16:25.854Z', modified: '2018-06-21T05:38:37.978Z' } }]
* match each response == { meta: '#(meta)' }

这篇关于使用#regex标记出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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