So why is this not working? I’m creating a regex that will match a formula (which is then part of a larger standard description). But I’m stuck here, as it doesn’t appear to want to match embedded formulas within a formula.
stat = /(Stat3|Stat2|Stat1)/
number_sym = /[0-9]*/
formula_sym = /((target's )?#{stat}|#{number_sym}|N#{number_sym})\%?/
math_sym = /(\+|\-|\*|\/|\%)/
formula = /^\((#{formula}|#{formula_sym})( #{math_sym} (#{formula}|#{formula_sym}))?\)$/
p "(target's Stat2 * N1%)".match(formula).to_s #matches
p "((target's Stat2 * N1%) + 3)".match(formula).to_s #no match
p "(Stat1 + ((target's Stat2 * N1%) + 3))".match(formula).to_s #no match
When you use the
#{ }syntax, Ruby converts the Regexp object to a string usingto_s. Look what happens when you convert a Regexp object to a string:To get the string you want (in my example, “blah”), use the
Regexp#sourcemethod:So to use your example: