S Ahmed
2012-Sep-30 23:16 UTC
How to escape a forward slash with gsub, also does interpolation work with gsub?
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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
bricker
2012-Sep-30 23:49 UTC
Re: How to escape a forward slash with gsub, also does interpolation work with gsub?
You have to escape the slashes and asterisk in the regex, as they are regular expression characters. http://rubular.com 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/-4Zgher1s3UJ. For more options, visit https://groups.google.com/groups/opt_out.
S Ahmed
2012-Oct-01 00:14 UTC
Re: Re: How to escape a forward slash with gsub, also does interpolation work with gsub?
thanks! BTW, is it possible that during the replace, it leaves the start/end tag? Currently it is replacing it also, I want the comments to be there after the replacement. On Sun, Sep 30, 2012 at 7:49 PM, bricker <bricker88-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You have to escape the slashes and asterisk in the regex, as they are > regular expression characters. http://rubular.com 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/-4Zgher1s3UJ. > For more options, visit https://groups.google.com/groups/opt_out. > > >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Jeffrey L. Taylor
2012-Oct-01 03:35 UTC
Re: How to escape a forward slash with gsub, also does interpolation work with gsub?
Quoting S Ahmed <sahmed1020-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> 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 */ ?Yes, text.gsub(/\/\* start \*\/(.*)\/\* end \*\//im, replace_with) Slightly simpler text.gsub(%r{/\* start \*/(.*)/\* end \*/}im, replace_with)> > 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) >gsub takes regex, not strings text.gsub(/#{start_tag}(.*)#{end_tag}/, replace_with) Special characters do need to be escaped in start_tag and end_tag, but in this case only the asterisks, not the slashes. HTH, Jeffrey -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.