Search code examples
cucumbercucumber-jvmcucumber-java

Cucumber 3: How to define a regular expression


In Cucumber 2 a regular expression is expressed as (.*) to get a parameter for the glue code. The expressions documentation of Cucumber 3 says that "You can use Regular Expressions or Cucumber Expressions." but sadly it does not mention the syntax.

In Cucumber 3 and 4 () expresses optional text and is equivalent to (?:) in Cucumber 2.

When () is reserved for optional text how can I define a regex in Cucumber 3? As {.*}?


Solution

  • Cucumber 3 introduced Cucumber Expressions. For simple use cases they serve as a more readable regular expression. Unfortunately Java does not have a syntax to distinguish regular expressions from strings. So to distinguish between the two Cucumber JVM uses a heuristic.

    • strings are cucumber expressions by default
    • ^definitely a regexp$
    • /surely a regexp/
    • this (.+) like a regexp
    • this look(s) like a cukexp

    Adding ^ and end markers $ is the surest way to get the regular expression behavior.