Skip to main content

Cucumber Step Definitions Regex Examples

Its obvious that many Steps has certain pattern, In that case we do not declare different Step Definition for each Step. We try to find the pattern and bring down the number of step definitions required.

Regular Expressions is a string of text that allows us to create patterns that help match, locate, and manage text. Using Regular expression we can optimize our step definition.

For statically typed languages (Java, C, C++), Cucumber automatically transforms strings into appropriate type but for dynamically typed languages (Perl, Ruby, Python), no transformation happens automatically. Therefore, its good idea to understand the regular expression pattern used in Cucumber.

Cucumber Step Definition Regular Expression

Consider below Step definition

@Given("^I follow \"([^\"]*)\"$")
public void i_follow_on_page(String link) 
{
    driver.findElement(By.linkText(link)).click();
}

Regular Expression in Cucumber Examples

Let's break it down and understand each char:

Pattern Step Step Definition Detail
.

And I see gray text

or

And I see grey text

@Then("I see (gr.y) text")

You can use any character in place of dot. In our example we have used and 
*

And I see hello text

And I see hellooooooooo text

@Then("I see (hello*) text")

A repetition modifier. It tells character can be repeated n number of times. We can use one or n times.
.*  

a

AbCD

punctuation!

123-456

an empty string

any character (except a newline) 0 or more times
.+  

a

AbCD

punctuation!

123-456

at least one of anything (except a newline)
\d     For Numbers [0-9]. Matches a single character that is digit.
\D     Not a Digit
[0-9]* or \d*  

123456

9

an empty string

matches a series of digits (or nothing)
[0-9]+ or \d+  

123456

9

matches one or more digits
.{2}  

ab

Ab

a!

25

exactly two of any character
.{1,3}  

a

Ab 

ab!

5

one to three of any character
\w     For Words. [A-Za-z0-9_]. Marches a word character, alphanumeric character plus underscore.
\W     Not a Word
\s     Matches white space characters including tab and line break.
\S     Not White Space
\b     Word Boundary
\B     Not a Word Boundary
?   colou?r matches colour or color. Makes proceeding token optional. 
|   cat|dog Equivalent to or. 
^   /^foo/ matches foo and foo bar but not bar foo beginning of string
$   I'm logged in will match I'm logged in with adminI'm logged in with editor. ^I'm logged$ in will match exact string.  End of string. 
"[^"]*"     matches something (or nothing) in double quotes
()     When you surround part of a regular expression with parentheses, it becomes a capture group. In a Cucumber Step Definitions, the text matched within each capture group is passed to the code block as an argument.

You can negate above characters by using capitalize version of it for example \D means any character except numbers.

Video Tutorial: Cucumber Regular Expression

Submitted by tgoswami on August 30, 2020

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.