Cucumber Regex:如何匹配逗号分隔值对的重复模式?
我有以下字符串:
I submit the following values: username 'foo', password 'bar'
I submit the following values: username 'foo', password 'bar','foo', profile 'bar', extra 'something'
我正在尝试匹配值对,但我不确定如何重复模式。
所以我想要的结果是:
username 'foo'
password 'bar'
...
到目前为止我的正则表达式:
I submit the following values: (\w+\s[^,]+),
我需要找到一种方法来重复该模式,并且我还需要处理末尾缺少的逗号。我正在类似 Cucumber 的 Python 测试框架中使用结果(刷新)。
最终结果将类似于:
@When(r'I submit the following values: (\w+\s[^,]+), ...')
def post_values_to_url(*args):
post_dict = {}
for pairs in args:
#add values to dict
response = client.get('this/is/a/url', post_dict)
I have the following strings:
I submit the following values: username 'foo', password 'bar'
I submit the following values: username 'foo', password 'bar','foo', profile 'bar', extra 'something'
I am trying to match the value pairs but I am not sure how I can repeat a pattern.
So the result I want is:
username 'foo'
password 'bar'
...
My regex so far:
I submit the following values: (\w+\s[^,]+),
I need to find a way to repeat the pattern and I also need to take care of the missing comma at the end. I am using the result in a Cucumber like testing framework for Python (freshen).
The end result will be something like:
@When(r'I submit the following values: (\w+\s[^,]+), ...')
def post_values_to_url(*args):
post_dict = {}
for pairs in args:
#add values to dict
response = client.get('this/is/a/url', post_dict)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您需要能够解析任意数量的对,那么我建议使用表,例如:
Cucumber::Ast::Table 上有一个 rows_hash 方法,可以满足您的需要。
但是,我怀疑使用像这样的“一刀切”步骤定义会使您的场景难以阅读。相反,做这样的事情怎么样:
编辑:刚刚注意到你实际上不是黄瓜。但我怀疑 Freshen 也会支持表格。
If you need to be able to parse an arbitrary number of pairs, then I'd suggest using a table, e.g.:
There's a rows_hash method on Cucumber::Ast::Table that will give you what you need.
However, I suspect that using a 'one-size-fits-all' step definition like this is going make your scenarios difficult to read. Instead, how about doing something like this:
Edit: Just noticed you're not actually Cucumber. But I suspect Freshen would also support tables.
环绕整个图案:
(?:(\w+\s[^,]+),)+
Surround the whole pattern :
(?:(\w+\s[^,]+),)+
最终将其分成两个正则表达式:
Ended up splitting it into two regex: