1

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):

[varA][varB]_begin[varC]
content line 1
content line 2
...
[varA][varB]_end[varC]

And I want to replace everything including the first varA and the last varC.

I put together a skeleton of the regular expression to match this. So, supposing, varA were "<%" and varB were "block" and varC were "%>", the regular expression would need to be:

/<%block_beg%>(.*)<%block_end%>/m/

I've confirmed that this pattern should work (via rubyxp)

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.

Given what I understand about Ruby's gsub(), in my estimation the following should work:

 gsub(/#{@varA}#{@varB}_beg#{@varC}(.*)#{@varA}#{@varB}_end#{@varC}/m, old_str)

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?

Post-Note 1: The @'s are due to the fact that the variables are class properties.

Post-Note 2: I've fixed a few things and it is still not working. I now have:

 @content.gsub(/#{Regexp.escape(@varA)}#{Regexp.escape(@varB)}_beg#{Regexp.escape(@varC)}(.*)#{Regexp.escape(@varA)}#{Regexp.escape(varB)}_end#{Regexp.escape(@varC)}/m)

Anyone know why that doesn't work?

4
  • Technically speaking in specific instance that I am working on, they should all have @'s, because they are properties of a class. Commented Apr 27, 2011 at 18:39
  • I'd recommend refactoring and replacing your ivars which you want interpolated with a Hash. Then just iterate through the hash and gsub every key with value. Commented Apr 27, 2011 at 19:22
  • First, create your regexp and assign it to a variable, and then p myregexp. See what regex you are actually running. My guess is that your instance variables are nil, whose #to_s is an empty string, and so your regex is /_beg(.*)_end/m. Commented Apr 27, 2011 at 19:42
  • The second argument is missing in your gsub under Post-Note 2. Commented Apr 27, 2011 at 20:53

1 Answer 1

2

Your gsub doesn't look quite right. This is how I would do it:

old_str.gsub(/#{varA}#{varB}_begin#{varC}(.*)#{varA}#{varB}_end#{varC}/m)

Also, if your variables are strings, you probably want to call Regexp.escape on them first, so that they are matched as literal strings rather than as regexp directives.

Sign up to request clarification or add additional context in comments.

1 Comment

See revisions to my original post (the Post-Note 1 and 2).. still having problems

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.