Cucumber Expression is cool but I prefer using Regular Expression

Cucumber Expressions was developed as an alternative to Regular Expressions with a more intuitive syntax. Cucumber now supports both Cucumber Expressions and Regular Expressions to define Step Definitions. It can determine if a step definition should be interpreted as a Cucumber Expression or a Regular Expression.

When there are no special characters, a Cucumber Expression is always assumed:

@When(“the user logs in with invalid credential”)

The presence of anchors assumes a Regular Expression, even if only one of the anchors are present:

@When(“^the user logs in with invalid credential$”)

Notes:

  • Cucumber Expression and Regular Expression can’t be used in the same step
  • If you use {string} as cucumber expression in step definition then in feature file you must pass the parameter in single quotes or double quotes. If you don’t want to add any quotes , use RegEx instead.
  • To use Regular Expressions, you can add an anchor (starting with ^ , ending with $) or forward slashes (/) or just define step parameter using RegEx syntax.

Please find more details of Cucumber Expression here . In general, It is simple to use and more intuitive syntax compared to Regular expression.

But Why I prefer using Regular Expression instead of Cucumber Expression ?

The only reason I prefer using Regular Expression instead of Cucumber Expression is because the IDE I am working on like Eclipse or IntelliJ do not support search with Cucumber Expression syntax. They only support Regular Expression.

I find this important as when refactoring the step, let’s say changing the step name, we need to find all the usage of this step in all feature files to adapt with the new step name. But using Cucumber Expression I am not able to do the usage search.

Eclipse IDE search box

As you can see above Eclipse search box, it only supports “Regular Expression” which is not compatible with Cucumber Expression.

Of course we can rewrite the Cucumber expression syntax to Regular Expression to search all step usage. There is a work around but I don’t think people feel comfortable with that.

Also, We don’t need to be master in Regular Expression syntax to use it in Cucumber feature files. I mostly use only simple String/Integer parameters, optional text. That’s all !

Leave a comment