Ruby on Rails Sunday, September 30, 2012

You have to escape the slashes and asterisk in the regex, as they are regular expression characters. is a great place to learn and play around with regular expressions.

    text.gsub(/\/\* start \*\/(.*)\/\* end \*\//im, replace_with)

Yes, interpolation works in regular expressions:

    text.gsub(/#{start_tag}(.*)#{end_tag}/, replace_with)

On Sunday, September 30, 2012 4:17:03 PM UTC-7, Gitted wrote:
I am trying this:

I want to replace all the text in between java comments:

/* start */
replace this text here
/* end */

I'm trying:

text.gsub(//* start */(.*)/* end *//im, replace_with)

But i'm getting errors relating to expecting keyword_end.

How should I be escaping /* and */   ?

Also, does string interpolation work in gsub regex?  Say I had variables like:

start_tag = "/* start */"
end_tag = "/* end */"

I want to do:

text.gsub("#{start_tag}(.*)#{end_tag}", replace_with)

You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to
To unsubscribe from this group, send email to
To view this discussion on the web visit
For more options, visit

No comments:

Post a Comment