tessl install tessl/maven-io-cucumber--cucumber-expressions@19.0.0Cucumber Expressions are simple patterns for matching Step Definitions with Gherkin steps
Cucumber Expressions is a Java library that provides an intuitive alternative to Regular Expressions for pattern matching in behavior-driven development (BDD) test automation. It enables developers to write readable step definitions for Gherkin scenarios using a simpler syntax with built-in parameter types (int, float, string, word) that automatically convert matched text to strongly-typed arguments.
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-expressions</artifactId>
<version>19.0.0</version>
</dependency>import io.cucumber.cucumberexpressions.*;
import java.util.*;
// Create registry and factory
ParameterTypeRegistry registry = new ParameterTypeRegistry(Locale.ENGLISH);
ExpressionFactory factory = new ExpressionFactory(registry);
// Create expression and match text
Expression expr = factory.createExpression("I have {int} cucumbers");
Optional<List<Argument<?>>> match = expr.match("I have 42 cucumbers");
// Extract value
if (match.isPresent()) {
Integer count = (Integer) match.get().get(0).getValue();
System.out.println("Count: " + count); // 42
}Create and match text patterns using Cucumber Expression or Regular Expression syntax.
package io.cucumber.cucumberexpressions;
public final class ExpressionFactory {
public ExpressionFactory(ParameterTypeRegistry parameterTypeRegistry);
public Expression createExpression(String expressionString);
}
public interface Expression {
Optional<List<Argument<?>>> match(String text, Type... typeHints);
Pattern getRegexp();
String getSource();
}→ Detailed Expression Reference
Define custom parameter types for automatic type conversion.
package io.cucumber.cucumberexpressions;
public final class ParameterType<T> {
public ParameterType(String name, String regexp, Class<T> type, Transformer<T> transformer);
public static <E extends Enum<E>> ParameterType<E> fromEnum(Class<E> enumClass);
}
public final class ParameterTypeRegistry {
public ParameterTypeRegistry(Locale locale);
public void defineParameterType(ParameterType<?> parameterType);
}Convert matched strings to Java types.
package io.cucumber.cucumberexpressions;
@FunctionalInterface
public interface Transformer<T> {
T transform(String arg) throws Throwable;
}
@FunctionalInterface
public interface CaptureGroupTransformer<T> {
T transform(String[] args) throws Throwable;
}Extract typed arguments with position information.
package io.cucumber.cucumberexpressions;
public final class Argument<T> {
public Group getGroup();
public T getValue();
public Type getType();
public ParameterType<T> getParameterType();
}
public final class Group {
public String getValue();
public int getStart();
public int getEnd();
}| Type | Syntax | Java Type | Example |
|---|---|---|---|
| Integer | {int} | Integer | 42, -19 |
| Float | {float} | Float | 3.6, -9.2 |
| Double | {double} | Double | 3.14159 |
| String | {string} | String | "hello", 'world' |
| Word | {word} | String | hello, user123 |
| Byte | {byte} | Byte | 127 |
| Short | {short} | Short | 1000 |
| Long | {long} | Long | 1234567890 |
| BigInteger | {biginteger} | BigInteger | 999999999999 |
| BigDecimal | {bigdecimal} | BigDecimal | 99.99 |
| Anonymous | {} | Object | Any text |
Simple, readable pattern syntax with parameter placeholders:
I have {int} cucumbers - matches integersI have {string} in my bag - matches quoted stringsI have {int} cucumber(s) - optional text with parenthesesin my belly/stomach - alternative text with forward slashExpressions starting with ^ or ending with $ use regex mode:
^I have (\\d+) cucumbers$ - regex with anchorsBuilt-in support for locale-specific number formats:
1,234.56 (comma thousands, period decimal)1.234,56 (period thousands, comma decimal)ParameterTypeRegistry(Locale)Automatically generate expressions from example text:
CucumberExpressionGenerator generator = new CucumberExpressionGenerator(registry);
List<GeneratedExpression> expressions = generator.generateExpressions("I have 42 cucumbers");
// Suggests: "I have {int} cucumbers"→ Expression Generation Reference
CRITICAL:
// Safe concurrent usage
public class ExpressionManager {
private final ExpressionFactory factory;
public ExpressionManager() {
ParameterTypeRegistry registry = new ParameterTypeRegistry(Locale.ENGLISH);
registry.defineParameterType(/* custom types */);
this.factory = new ExpressionFactory(registry);
}
// Thread-safe
public Optional<List<Argument<?>>> match(String pattern, String text) {
Expression expr = factory.createExpression(pattern);
return expr.match(text);
}
}All exceptions extend CucumberExpressionException:
→ Exception Handling Reference
Expression expr = factory.createExpression("User {string} logs in with {string}");
Optional<List<Argument<?>>> match = expr.match("User \"john@example.com\" logs in with \"secret123\"");ParameterType<Email> emailType = new ParameterType<>(
"email", "[\\w.]+@[\\w.]+", Email.class, Email::new
);
registry.defineParameterType(emailType);enum Status { ACTIVE, INACTIVE }
ParameterType<Status> statusType = ParameterType.fromEnum(Status.class);
registry.defineParameterType(statusType);ConcurrentHashMap for frequently used patterns| Regular Expression | Cucumber Expression |
|---|---|
(\\d+) | {int} |
(.+) | {string} |
(\\S+) | {word} |
(\\d+\\.\\d+) | {float} |
import io.cucumber.cucumberexpressions.ExpressionFactory;
import io.cucumber.cucumberexpressions.ParameterTypeRegistry;
import io.cucumber.cucumberexpressions.ParameterType;
import io.cucumber.cucumberexpressions.Expression;
import io.cucumber.cucumberexpressions.Argument;
import io.cucumber.cucumberexpressions.Transformer;ParameterTypeRegistry with localeExpressionFactory with registryNeed Help?