当Ruby正则表达不适合在线

Sir*_*lot 10 ruby regex cucumber

当我有一个非常长的正则表达式,如黄瓜步骤定义,什么是最好的线包装方式?

例如,我想要像:

When /^I have a very long step definition here in my step definition file$/ do
  ...
end
Run Code Online (Sandbox Code Playgroud)

分成两行(这不起作用:)

When /^I have a very long step definition here in /\
    /my step definition file$/ do
  ...
end
Run Code Online (Sandbox Code Playgroud)

2018年更新

如果你专门用于黄瓜,使用黄瓜表达是正则表达的一个很好的选择

Tim*_*ker 14

您可以使用带修饰符的详细正则表达式/x,但是您需要将空格显式化,否则将忽略它们.另一个优点是,这允许您评论您的正则表达式(如果它很长,可能是一个好主意):

/^                               # Match start of string
I[ ]have[ ]a[ ]very[ ]long[ ]
step[ ]definition[ ]here[ ]
in[ ]my[ ]step[ ]definition[ ]file
$                               # Match end of string
/x
Run Code Online (Sandbox Code Playgroud)