I am trying to use REGEX to parse a gherkin file. I have successfully split it into multiple REGEX's to accomplish what I want, but I am having a problem getting the last instance of one of the Values.
(?s)(?P<Then>Then.*?)Scenario:|$
returns all instances except the last from
# This is a sample .feature file
Feature: Authorized Logins
Users will have individual logins, gated by passwords.
Scenario: Invalid login credentials are entered
Given Default Database is loaded
And App is available and running
When User enters invalid login
Then Application should deny login
Scenario: Valid login credentials are entered
Given Default Database is loaded
And App is available and running
When User enters valid login
And display test case selection screen
Then Application should grant login
And display test case selection screen
Scenario: No database connection
Given Database is stopped
And App is available and running
When User enters valid login
Then Application will deny login
And inform of no connection
The last
Then Application will deny login
And inform of no connection
Doesn't get selected. I have tried various things, but can't seem to get it. Any suggestions?
Right now your regex is saying: Match either (?P<Then>Then.*?)Scenario:
or $
, you need to group all options together as shown below.
(?P<Then>Then.*?)(?:Scenario:|$)
You can also use the s
modifier ( re.DOTALL
) instead of placing it in the regex as (?s)
.
Regular expressions is a very tempting tool when it comes to parsing data. But, some data has a pre-defined structure and format rules. And, most importantly, existing parsers.
In your case, these are the BDD Gherkin feature
files. behave
library has an already existing parser - install behave
, import and use it, sample:
from behave.parser import Parser
filename = "test.feature"
with open(filename) as f:
feature = Parser().parse(f.read(), filename)
print(feature.name)
for scenario in feature.scenarios:
print(scenario.name)
print("-----")
for step in scenario.steps:
print(step.keyword, step.name)
print("--------")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.