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

时间:2018-05-02 18:28:07

标签: c# regex specflow

我想编写步骤定义,这将适用于以下三个步骤之一。 我需要在双引号中选择最后两个参数。

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 ""(.*)"", [""(.*)""]?, [""(.*)""]?")]

谢谢。

2 个答案:

答案 0 :(得分:2)

我相信这会匹配/分组您要匹配的内容

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

Regexr Example

答案 1 :(得分:1)

正则表达式:

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)