Cucumber Regex:如何匹配逗号分隔值对的重复模式?

发布于 2024-11-02 13:20:11 字数 751 浏览 1 评论 0原文

我有以下字符串:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逆流 2024-11-09 13:20:11

如果您需要能够解析任意数量的对,那么我建议使用表,例如:

When I submit the following values:
  | username | foo |
  | password | bar |

Cucumber::Ast::Table 上有一个 rows_hash 方法,可以满足您的需要。

但是,我怀疑使用像这样的“一刀切”步骤定义会使您的场景难以阅读。相反,做这样的事情怎么样:

Given a user exists with username "foo"
And that user has the profile "bar"
When I login as "foo" with password "bar"
...

编辑:刚刚注意到你实际上不是黄瓜。但我怀疑 Freshen 也会支持表格。

If you need to be able to parse an arbitrary number of pairs, then I'd suggest using a table, e.g.:

When I submit the following values:
  | username | foo |
  | password | bar |

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:

Given a user exists with username "foo"
And that user has the profile "bar"
When I login as "foo" with password "bar"
...

Edit: Just noticed you're not actually Cucumber. But I suspect Freshen would also support tables.

ゃ懵逼小萝莉 2024-11-09 13:20:11

环绕整个图案:
(?:(\w+\s[^,]+),)+

Surround the whole pattern :
(?:(\w+\s[^,]+),)+

反差帅 2024-11-09 13:20:11

最终将其分成两个正则表达式:

@When(r'I submit the following values: (.+)')
def post_values_to_url(pairs):
    regex = r'(\w+)\s\'(\w+)\',?'
    results = re.findall(regex, pairs)
    post_dict = {}

    for result in results:
        post_dict[result[0]] = result[1]

Ended up splitting it into two regex:

@When(r'I submit the following values: (.+)')
def post_values_to_url(pairs):
    regex = r'(\w+)\s\'(\w+)\',?'
    results = re.findall(regex, pairs)
    post_dict = {}

    for result in results:
        post_dict[result[0]] = result[1]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文