{"id":2450,"library":"cucumber-expressions","title":"Cucumber Expressions for Python","description":"Cucumber Expressions is an alternative to Regular Expressions with a more intuitive syntax, designed for defining step definitions in a human-readable format. This Python library provides the parsing and matching capabilities for these expressions. The current version is 19.0.0, and it maintains an active development status with regular updates, including recent Python-specific changes.","status":"active","version":"19.0.0","language":"en","source_language":"en","source_url":"https://github.com/cucumber/cucumber-expressions.git","tags":["BDD","testing","cucumber","expressions","gherkin"],"install":[{"cmd":"pip install cucumber-expressions","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"Expression","correct":"from cucumber_expressions.expression import Expression"},{"symbol":"ParameterType","correct":"from cucumber_expressions.parameter_type import ParameterType"},{"symbol":"ParameterTypeRegistry","correct":"from cucumber_expressions.parameter_type import ParameterTypeRegistry"}],"quickstart":{"code":"from cucumber_expressions.expression import Expression\nfrom cucumber_expressions.parameter_type import ParameterType, ParameterTypeRegistry\n\n# --- Basic Expression Matching ---\n# Create a registry (required even for built-in types)\nregistry = ParameterTypeRegistry()\n\n# Define an expression with a built-in parameter type\nexpression_int = Expression(\"I have {int} cucumbers in my belly\", registry)\nmatch_int = expression_int.match(\"I have 42 cucumbers in my belly\")\nprint(f\"Match for 'I have 42 cucumbers': {match_int.group_values if match_int else None}\")\n\nmatch_float = expression_int.match(\"I have 3.14 cucumbers in my belly\")\nprint(f\"Match for 'I have 3.14 cucumbers' (expects int): {match_float.group_values if match_float else None}\")\n\n# --- Custom Parameter Type ---\nclass Color:\n    def __init__(self, name):\n        self.name = name\n    def __repr__(self):\n        return f\"Color('{self.name}')\"\n\n# Define a custom parameter type and add to the registry\ncolor_parameter_type = ParameterType(\n    name=\"color\",\n    regexp=\"red|blue|yellow\",\n    type=Color,\n    transformer=lambda s: Color(s)\n)\nregistry.define_parameter_type(color_parameter_type)\n\n# Use the custom parameter type in an expression\nexpression_color = Expression(\"I have a {color} ball\", registry)\nmatch_color = expression_color.match(\"I have a red ball\")\nprint(f\"Match for 'I have a red ball': {match_color.group_values if match_color else None}\")\n\nmatch_color_fail = expression_color.match(\"I have a green ball\")\nprint(f\"Match for 'I have a green ball': {match_color_fail.group_values if match_color_fail else None}\")","lang":"python","description":"This quickstart demonstrates how to create basic Cucumber Expressions using built-in parameter types like `{int}` and how to define and register your own custom parameter types (`{color}` in this example) to extend the expression matching capabilities. It shows how to initialize `Expression` objects with a `ParameterTypeRegistry` and use the `match` method to extract values."},"warnings":[{"fix":"Upgrade Python to version 3.10 or higher.","message":"Support for Python 3.7, 3.8, and 3.9 has been removed in versions 17.0.0 and 18.0.0 respectively. Users on these older Python versions must upgrade their Python environment to 3.10 or newer to use `cucumber-expressions` version 19.0.0 and above.","severity":"breaking","affected_versions":">=17.0.0"},{"fix":"Use either pure Cucumber Expression syntax or pure Regular Expression syntax (if supported by your test runner, like `behave`), but do not combine them in one expression string parsed by this library.","message":"Mixing Cucumber Expression syntax with Regular Expression syntax within a single expression string is not supported. Cucumber Expressions use their own intuitive syntax (e.g., `{int}`, `(s)`, `belly/stomach`) and interpret characters like `(`, `)`, `{`, `}` differently from regular expressions.","severity":"gotcha","affected_versions":"All"},{"fix":"If you need to extract values, define them as a parameter type (e.g., `{word}` or `{string}`). If a part of the text is optional, simply enclose it in parentheses.","message":"In Cucumber Expressions, parentheses `()` denote optional text, not capture groups as they do in regular expressions (e.g., `cucumber(s)` matches 'cucumber' or 'cucumbers', but 's' is not extracted as a separate value). Use explicit parameter types like `{word}` or `{string}` for value extraction.","severity":"gotcha","affected_versions":"All"},{"fix":"Escape special characters `(`, `{`, `\\` with a preceding backslash. Consider raw strings (`r'...'`) or double backslashes in Python string literals to simplify escaping.","message":"To match literal `(`, `{`, or `\\` characters within a Cucumber Expression, they must be escaped with a backslash (`\\`) (e.g., `\\{text}` will match `{text}`). Depending on the Python string literal definition, you might need to use a double backslash (`\\\\`) in your code to ensure a single backslash is passed to the expression parser.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure no whitespace exists immediately before or after the `/` character when defining alternative text within a Cucumber Expression (e.g., `I have {int} cucumber(s) in my belly/tummy`).","message":"Alternative text using the `/` character (e.g., `belly/stomach`) only works when there is no whitespace between the alternative parts. `belly / stomach` would be treated as literal text, not alternatives.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}