如何在 Ruby 正则表达式中插入变量? [英] How can I interpolate a variable in a Ruby regex?
本文介绍了如何在 Ruby 正则表达式中插入变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
data.to_enum(:scan,/(#entity[0])/i).map do |m,|
p $`.size
如何在正则表达式中使用动态变量?#entity[0]
返回一个值,但在上面的语法中,#entity[0] 在正则表达式中按字面意义使用.
How can I use dynamic variable in regular expression?
#entity[0]
returns a value, but in the above syntax #entity[0] is taken literally in the regex.
推荐答案
你想要 /#{entity[0]}/i
.#{}
是在字符串和正则表达式中插入变量的语法.
You want /#{entity[0]}/i
. #{}
is the syntax for variable insertion in strings and regexes.
这篇关于如何在 Ruby 正则表达式中插入变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文