Alejandro Huerta
Alejandro Huerta

Reputation: 1144

Regex expression to match all cucumber steps and grab double quotes

As some background, I am attempting to write a generalized step that will match steps such as

When I "click" on "send" button
When I "press" on that "clear" thingy
When I "select" some kind of "money" maker

and give me what is in quotes.

Optimally the step will look something like

When(/regex here/) do |action, target|
   #do something here
end

I have tried(.*)"(.*)"(.*)"(.*)"(.*) and it does work but would require me to write the step such as

When(/(.*)"(.*)"(.*)"(.*)"(.*)/) do |unused, action, unused, target, unused|
   #do something here
end

another side effect is the entire step in the .feature file becomes highlighted, which is minor but would be nice to know exactly what is being grabbed, which is what is in double quotes.

What would be the Regex expression to accomplish this goal?

Upvotes: 1

Views: 1113

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 149020

Try using [^"] to match anything except " characters and only specify the groups you want to capture. For example:

When(/"([^"]*)"[^"]*"([^"]*)"/) do |action, target|
   #do something here
end

Upvotes: 3

Related Questions