将Ruby正则表达式拆分为多行 [英] Split Ruby regex over multiple lines

查看:56
本文介绍了将Ruby正则表达式拆分为多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能不是您所期望的问题!我不希望能在换行符上匹配的正则表达式;相反,我想写一个长的正则表达式,为了便于阅读,我想分成多行代码。

This might not be quite the question you're expecting! I don't want a regex that will match over line-breaks; instead, I want to write a long regex that, for readability, I'd like to split onto multiple lines of code.

类似的东西:

"bar" =~ /(foo|
           bar)/  # Doesn't work!
# => nil. Would like => 0

可以做到吗?

推荐答案

您需要使用 / x 修饰符,该修饰符启用免费模式

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

在您的情况下:

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

这篇关于将Ruby正则表达式拆分为多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆