[rspec-users] embedding variable in a regex

Zach Dennis zach.dennis at gmail.com
Fri Aug 8 11:20:52 EDT 2008

On Fri, Aug 8, 2008 at 10:18 AM, aidy lewis <aidy.lewis at googlemail.com> 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|

Which should match the step with the name

   Given "a PROGRAM title of 'foo bar baz thingy majoo'"

Zach Dennis

More information about the rspec-users mailing list