Note that there are some explanatory texts on larger screens.

plurals
  1. PODifficulties with using variables with Ruby gsub call for block replace
    primarykey
    data
    text
    <p>What I'm trying to accomplish is replacing a block of text (not terminated by new lines) in a template-ish way. The block I want to replace looks something like this (to make this more readable, I've put variable values in block brackets):</p> <pre><code>[varA][varB]_begin[varC] content line 1 content line 2 ... [varA][varB]_end[varC] </code></pre> <p>And I want to replace everything including the first varA and the last varC.</p> <p>I put together a skeleton of the regular expression to match this. So, supposing, varA were "&lt;%" and varB were "block" and varC were "%>", the regular expression would need to be:</p> <pre><code>/&lt;%block_beg%&gt;(.*)&lt;%block_end%&gt;/m/ </code></pre> <p>I've confirmed that this pattern should work (via rubyxp)</p> <p>But of course its not that easy because the values we used for varA, varB, and varC in the previous examples need to be dynamically supplied.</p> <p>Given what I understand about Ruby's gsub(), in my estimation the following should work:</p> <pre><code> gsub(/#{@varA}#{@varB}_beg#{@varC}(.*)#{@varA}#{@varB}_end#{@varC}/m, old_str) </code></pre> <p>The code runs silently without throwing errors, but also doesn't replace anything. Can anyone tell me why this isn't working? Should I be taking a different approach?</p> <p>Post-Note 1: The @'s are due to the fact that the variables are class properties.</p> <p>Post-Note 2: I've fixed a few things and it is still not working. I now have:</p> <pre><code> @content.gsub(/#{Regexp.escape(@varA)}#{Regexp.escape(@varB)}_beg#{Regexp.escape(@varC)}(.*)#{Regexp.escape(@varA)}#{Regexp.escape(varB)}_end#{Regexp.escape(@varC)}/m) </code></pre> <p>Anyone know why that doesn't work?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload