liveBook · Manning

Regular expression

As explained in BDD in Action, Second Edition

A regular expression is a pattern notation for matching and extracting text according to specified character and repetition rules.

A regular expression is a concise pattern notation for matching scenario text and extracting parameter values in Cucumber step definitions. It can make steps flexible and reusable, but complex patterns are difficult to understand. section 8.4.4

Use regular expressions to:

  • Capture arbitrary text with `(.*)` or require at least one character with `(.+)`. section 8.4.4
  • Restrict values with digit patterns, exact lengths, alternatives, optional characters, and non-capturing groups. section 8.4.4
  • Match parameters directly inside step definitions, including statuses, names, and point totals. section 8.4.4

Matching a frequent-flyer step

The pattern `^Tara is a (.*) Frequent Flyer member with (\\d+) points` captures the member status and requires a sequence of digits for the points value.

Constraining values

Alternatives such as `(Standard|Bronze|Silver|Gold)` accept only known statuses, while `s?he` accepts either “she” or “he.”

Table 8.2 Common regular expressions used in Cucumber scenarios (view table figure)

Regular Expression

Description

Example

Matches

.*

Any sequence of characters (including nothing)

^ (.*) is a frequent flyer

Tara is a frequent flyer

.+

A sequence of one or more characters

^ (.+) is a frequent flyer

Tara is a frequent flyer

/d+

A sequence of one or more digits

Tara is (/d+) years old

Tara is 30 years old

/d{n}

A sequence of precisely n digits

In the year /d{4}

In the year 2020

(a|b|c)

Any one of the listed values

The status is (red|amber|green)

The status is red

The status is amber

The status is green

?

An optional character

s?he purchases a ticket

she purchases a ticket

he purchases a ticket

?:

A non-capturing group

Tara (?:buys|has bought) a ticket

Tara buys a ticket

Tara has bought a ticket

Browse common Cucumber regular-expression forms with their meanings and matching examples. section 8.4.4

How BDD in Action, Second Edition covers it

How deep it goes
Explains anchors, capture groups, wildcards, character classes, alternatives, optional characters, and non-capturing groups with examples.
Examples
Uses frequent-flyer statuses, point totals, pronoun variants, registration wording, and ISO-formatted dates.
What it assumes
Assumes familiarity with Cucumber scenarios and step definitions while introducing the required regular-expression syntax through examples.
What you can do after
Apply regular expressions to implement reusable Cucumber step definitions with constrained and captured parameters.
How it connects
Connects regular expressions to Cucumber Expressions, step definitions, custom parameter types, and automated acceptance tests.

Also explained in

Compare how 27 books explain regular expression →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of BDD in Action, Second Edition. The section links point to the parts of the book it draws on.