双引号中的 SpecFlow C# Regex 可选参数

SpecFlow C# Regex optional parameter in double quotes

我想编写步骤定义,它适用于以下三个步骤中的任何一个。 我需要在双引号中将最后两个参数设为可选。

Given I do "x"
Given I do "x", "y "
Given I do "x", "y ", " z"

我试过几种表达方式:

[Given(@"I do ""(.*)"", ""(.*)?"", ""(.*)?""")]
[Given(@"I do ""(.*)"", ""(.*)""?, ""(.*)""?")]
[Given(@"I do ""(.*)"", (""(.*)"")?, (""(.*)"")?")]
[Given(@"I do ""(.*)"", [""(.*)""]?, [""(.*)""]?")]

谢谢。

我相信这个matches/groups你想要匹配的东西

I do \"(.*?)\"(?:, \"(.*?)\")?(?:, \"(.*?)\")?

Regexr Example

正则表达式:

I do \".*?\"(?:, \".*?\")*

Demo

解释:

/I do \".*?\"(?:, \".*?\")*/
    I do  matches the characters I do  literally (case sensitive)
    \" matches the character " literally (case sensitive)
    .*? matches any character (except for line terminators)
    *? Quantifier — Matches between zero and unlimited times, as few times as possible, expanding as needed (lazy)
    \" matches the character " literally (case sensitive)
    Non-capturing group (?:, \".*?\")*
    * Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
        ,  matches the characters ,  literally (case sensitive)
        \" matches the character " literally (case sensitive)
        .*? matches any character (except for line terminators)
        \" matches the character " literally (case sensitive)