or run

tessl search
Log in

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/io.cucumber/cucumber-expressions@19.0.x

docs

index.md
tile.json

tessl/maven-io-cucumber--cucumber-expressions

tessl install tessl/maven-io-cucumber--cucumber-expressions@19.0.0

Cucumber Expressions are simple patterns for matching Step Definitions with Gherkin steps

quick-start.mddocs/guides/

Quick Start Guide

This guide will get you started with Cucumber Expressions in minutes.

Installation

Add the dependency to your pom.xml:

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-expressions</artifactId>
    <version>19.0.0</version>
</dependency>

For Gradle:

implementation 'io.cucumber:cucumber-expressions:19.0.0'

Basic Usage

Step 1: Import Required Classes

import io.cucumber.cucumberexpressions.*;
import java.util.Locale;
import java.util.List;
import java.util.Optional;

Step 2: Create Registry and Factory

// Create parameter type registry with locale
ParameterTypeRegistry registry = new ParameterTypeRegistry(Locale.ENGLISH);

// Create expression factory
ExpressionFactory factory = new ExpressionFactory(registry);

Step 3: Create an Expression

// Create expression from string
Expression expression = factory.createExpression("I have {int} cucumbers in my belly");

Step 4: Match Text

// Match text against expression
String text = "I have 42 cucumbers in my belly";
Optional<List<Argument<?>>> match = expression.match(text);

Step 5: Extract Values

// Extract typed value
if (match.isPresent()) {
    List<Argument<?>> arguments = match.get();
    Integer cucumberCount = (Integer) arguments.get(0).getValue();
    System.out.println("Number of cucumbers: " + cucumberCount); // 42
}

Complete Example

import io.cucumber.cucumberexpressions.*;
import java.util.*;

public class CucumberExpressionsExample {
    public static void main(String[] args) {
        // Step 1: Setup
        ParameterTypeRegistry registry = new ParameterTypeRegistry(Locale.ENGLISH);
        ExpressionFactory factory = new ExpressionFactory(registry);
        
        // Step 2: Create expression
        Expression expr = factory.createExpression("I have {int} cucumbers in my belly");
        
        // Step 3: Match and extract
        Optional<List<Argument<?>>> match = expr.match("I have 42 cucumbers in my belly");
        
        if (match.isPresent()) {
            Integer count = (Integer) match.get().get(0).getValue();
            System.out.println("Cucumber count: " + count);
        } else {
            System.out.println("No match found");
        }
    }
}

Using Multiple Parameters

// Expression with multiple parameters
Expression expr = factory.createExpression(
    "User {string} has {int} items worth {float} dollars"
);

// Match text
Optional<List<Argument<?>>> match = expr.match(
    "User \"John Doe\" has 5 items worth 99.99 dollars"
);

if (match.isPresent()) {
    List<Argument<?>> args = match.get();
    String username = (String) args.get(0).getValue();
    Integer itemCount = (Integer) args.get(1).getValue();
    Float totalValue = (Float) args.get(2).getValue();
    
    System.out.println("User: " + username);        // John Doe
    System.out.println("Items: " + itemCount);      // 5
    System.out.println("Value: $" + totalValue);    // $99.99
}

Custom Parameter Types

Step 1: Define the Type

// Simple custom type
ParameterType<Color> colorType = new ParameterType<>(
    "color",                    // Name (used as {color} in expressions)
    "red|blue|green|yellow",    // Regex pattern
    Color.class,                // Java type
    (String s) -> new Color(s)  // Transformer
);

Step 2: Register the Type

registry.defineParameterType(colorType);

Step 3: Use in Expressions

Expression expr = factory.createExpression("I have a {color} ball");
Optional<List<Argument<?>>> match = expr.match("I have a red ball");

if (match.isPresent()) {
    Color color = (Color) match.get().get(0).getValue();
    System.out.println("Color: " + color); // Color.RED
}

Enum Parameter Types

// Define enum
enum Status { ACTIVE, INACTIVE, PENDING }

// Create parameter type from enum
ParameterType<Status> statusType = ParameterType.fromEnum(Status.class);
registry.defineParameterType(statusType);

// Use in expression
Expression expr = factory.createExpression("Request is {Status}");
Optional<List<Argument<?>>> match = expr.match("Request is ACTIVE");

if (match.isPresent()) {
    Status status = (Status) match.get().get(0).getValue();
    System.out.println("Status: " + status); // Status.ACTIVE
}

Optional and Alternative Text

Optional Text

// Use parentheses for optional text
Expression expr = factory.createExpression("I have {int} cucumber(s)");

// Matches both singular and plural
expr.match("I have 1 cucumber");   // Matches
expr.match("I have 42 cucumbers"); // Matches

Alternative Text

// Use forward slash for alternatives
Expression expr = factory.createExpression("I have {int} in my belly/stomach");

expr.match("I have 42 in my belly");   // Matches
expr.match("I have 42 in my stomach"); // Matches

Regular Expression Mode

// Expressions starting with ^ or ending with $ use regex mode
Expression regexExpr = factory.createExpression("^I have (\\d+) cucumbers$");

Optional<List<Argument<?>>> match = regexExpr.match("I have 42 cucumbers");
// The \d+ pattern is automatically converted to Integer type

if (match.isPresent()) {
    Integer count = (Integer) match.get().get(0).getValue();
    System.out.println("Count: " + count); // 42
}

Error Handling

try {
    // Try to use undefined parameter type
    Expression expr = factory.createExpression("I have a {color} ball");
} catch (UndefinedParameterTypeException e) {
    System.err.println("Parameter type not found: " + e.getUndefinedParameterTypeName());
    // Register the missing type
    registry.defineParameterType(colorType);
    // Retry
    Expression expr = factory.createExpression("I have a {color} ball");
}

Expression Caching

For better performance, cache expressions:

import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;

public class ExpressionCache {
    private final ExpressionFactory factory;
    private final Map<String, Expression> cache = new ConcurrentHashMap<>();
    
    public ExpressionCache(ExpressionFactory factory) {
        this.factory = factory;
    }
    
    public Expression getExpression(String expressionString) {
        return cache.computeIfAbsent(expressionString, factory::createExpression);
    }
}

// Usage
ExpressionCache cache = new ExpressionCache(factory);
Expression expr = cache.getExpression("I have {int} cucumbers");

Thread Safety

// Safe concurrent usage pattern
public class SafeExpressionManager {
    private final ExpressionFactory factory;
    
    public SafeExpressionManager() {
        // Initialize registry and factory ONCE at startup
        ParameterTypeRegistry registry = new ParameterTypeRegistry(Locale.ENGLISH);
        
        // Register all custom types BEFORE concurrent use
        registry.defineParameterType(/* custom type */);
        
        this.factory = new ExpressionFactory(registry);
    }
    
    // Thread-safe: factory is immutable after construction
    public Optional<List<Argument<?>>> matchText(String pattern, String text) {
        Expression expr = factory.createExpression(pattern);
        return expr.match(text);
    }
}

Built-In Parameter Types Reference

TypeSyntaxExample Match
int{int}42, -19
float{float}3.6, -9.2
double{double}3.14159
string{string}"hello world"
word{word}hello
byte{byte}127
short{short}1000
long{long}1234567890
biginteger{biginteger}999999999999
bigdecimal{bigdecimal}99.99

Next Steps

  • Common Patterns - Learn frequently used patterns
  • Real-World Scenarios - See complete examples
  • Parameter Types Reference - Detailed type system documentation
  • Expression Reference - Complete expression syntax guide

Troubleshooting

Issue: UndefinedParameterTypeException

Problem: Expression uses {color} but type not registered.

Solution:

ParameterType<Color> colorType = new ParameterType<>(
    "color", "red|blue|green", Color.class, Color::new
);
registry.defineParameterType(colorType);

Issue: Pattern Doesn't Match

Problem: Extra whitespace in text breaks match.

Solution: Whitespace is significant in expressions. Ensure text matches exactly:

Expression expr = factory.createExpression("I have {int} cucumbers");
expr.match("I have 42 cucumbers");    // ✓ Matches
expr.match("I have  42  cucumbers");  // ✗ Doesn't match (extra spaces)

Issue: Case Sensitivity

Problem: Case mismatch breaks match.

Solution: Matches are case-sensitive by default:

Expression expr = factory.createExpression("I have {int} cucumbers");
expr.match("I have 42 cucumbers");  // ✓ Matches
expr.match("I HAVE 42 CUCUMBERS");  // ✗ Doesn't match (case mismatch)

Summary

You've learned how to:

  • ✅ Install and import Cucumber Expressions
  • ✅ Create expressions and match text
  • ✅ Extract typed values from matches
  • ✅ Define custom parameter types
  • ✅ Use enum types
  • ✅ Handle optional and alternative text
  • ✅ Cache expressions for performance
  • ✅ Use thread-safe patterns

Ready to dive deeper? Check out Common Patterns or explore Real-World Scenarios.