Split Ruby regex over multiple lines

故事扮演 提交于 2019-11-28 21:00:16
SilentGhost

You need to use the /x modifier, which enables free-spacing mode.

In your case:

"bar" =~ /(foo|
           bar)/x
mthorley

Using %r with the x option is the prefered way to do this.

See this example from the github ruby style guide

regexp = %r{
  start         # some text
  \s            # white space char
  (group)       # first group
  (?:alt1|alt2) # some alternation
  end
}x

regexp.match? "start groupalt2end"

https://github.com/github/rubocop-github/blob/master/STYLEGUIDE.md#regular-expressions

you can use:

"bar" =~ /(?x)foo|
         bar/
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!