黄瓜中步骤定义的正则表达式

Regex for step definiton in Cucumber

我正在尝试使用 Regex 将方法与我的步骤定义相匹配。 我希望步骤与以下任何一项匹配并捕获数字组。

  1. 燃料百分比是 74
  2. 燃油百分比应为 74
  3. 燃料百分比是 74

这是我正在使用的正则表达式 @Then(".\bfuel\b.\bpercent\b.* (\d+)")

但它似乎不匹配或捕获该值。 它说请执行缺少的步骤

@Then("the fuel percent should be {int}")
public void the_fuel_percent_should_be(Integer int1) {
    // Write code here that turns the phrase above into concrete actions
    throw new cucumber.api.PendingException();
}

你的步骤定义有误。您需要将 {int} 更改为 (\\d+)。 (正则表达式 \d+ 匹配数字)。所以它看起来像这样:

@Then("(fuel|the fuel) percent (is|should be) (\d+)")

匹配您给出的 3 个示例的正则表达式为:

.*fuel.*percent.*\d{1,3}

小黄瓜:

the fuel percent is '74'
the fuel percent should be '74' fuel
percent is '74'

Java:

@Then(".*fuel percent (is|should be) '(.*)'")
public void the_fuel_percent_should_be(String type, Integer int1) {
    // Write code here that turns the phrase above into concrete actions
    throw new cucumber.api.PendingException();
}

正则表达式在线演示 here.