Embedding variable in a regex

Given /a (PROGRAM|PROGRAMMES) title of ‘$title’/ do |title|

end

Could anyone give me the correct syntax for embedding this variable in
a regex?

Aidy

On Fri, Aug 8, 2008 at 10:18 AM, aidy lewis [email protected]
wrote:

Given /a (PROGRAM|PROGRAMMES) title of ‘$title’/ do |title|

end

Could anyone give me the correct syntax for embedding this variable in a regex?

You’ll need to create a sub-expression capturing the title.
Sub-expressions are denoted in a regular expression with
parenthesizes. You already have one in your with (PROGRAM|PROGRAMMES).
If your regex matched a given step the title would be either PROGRAM
or PROGRAMMES depending on what the step description looked like.

You probably want to use a non-matching sub-expression for
PROGRAM|PROGRAMMES so it doesn’t get captured (and thus passed in as a
block parameter).

Try this:

Given /a (?:PROGRAM|PROGRAMMES) title of ‘([^’]+)'/ do |title|
end

Which should match the step with the name

Given “a PROGRAM title of ‘foo bar baz thingy majoo’”


Zach D.
http://www.continuousthinking.com