1

I have a cucumber feature file that contains multiple scenarios and each has a different And. I am using Java regex to parse the file and want to pull out a specific scenario based on the text in the And.

For example:

Feature: testing stuff

Scenario:

lady swallows fly Given a lady and lady swallows a "fly" then lady lives

Scenario:

lady swallows spider Given a lady and lady swallows a "fly","spider" then lady lives

Scenario:

lady swallows everything Given a lady and lady swallows a "fly","spider","cat","dog","cow" then lady Dies

Scenario:

lady swallows everything except the dog Given a lady and lady swallows a "fly","spider","cat","cow" then lady Dies

I want to pull out the scenario that contains dog and dump it in a separate file. In my real life dog=account number and associates to multiple SQL extracts which are auto-loaded into a local database. This is a single scenario, in a single feature file, which is one of hundreds. So it's not a simple case of using @run at the top of the scenario. I am actually extracting the scenario and associated files so they can be put in a sandbox.

The regex that I have so far is:

.*?dog.*Scen

but that selects from the first instance of Scenario into the Scenario beyond what I want.

Instead I want to pull out the group:

Scenario:

lady swallows everything Given a lady and lady swallows a "fly","spider","cat","dog","cow" then lady Dies

Does anyone know how I can pull out that group?

1
  • Is each scenario contained in one continuous String? Or is there at least a "\n" delimiting them? Commented Aug 6, 2015 at 14:02

1 Answer 1

1

If scenarios are delimited by \n, try with:

^[^\n]+\bdog\b[^\n]+

DEMO

if it is contiuous string, try with:

(?=Scenario).+?\bdog\b.+?(?=Scenario|$)

DEMO

Sign up to request clarification or add additional context in comments.

1 Comment

Add word boundaries and you're good to go. These regexes will match strings that have the letters "dog" in it and not the word.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.