I''m trying to find a block of text within <pre> text </pre> tags, and replace it with nothing. However, I''m not experienced with regular expressions. Here''s what I want to do. s = "Here are some words. <pre> this is a code snippet </pre> More words." s.gsub([regex_goes_here],'''') Now, s becomes: $: s => "Here are some words. More words." I just don''t know the regular expression for this. Is it something like... s.gsub(/^[:<pre>:][:</pre>:]$, '''') ??? That doesn''t seem to work. Any help on this? Thanks in advance. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
gsub(/<pre>.*<\/pre>/, "<pre></pre>") ryan wrote:> I''m trying to find a block of text within <pre> text </pre> tags, and > replace it with nothing. However, I''m not experienced with regular > expressions. Here''s what I want to do. > > s = "Here are some words. <pre> this is a code snippet </pre> More > words." > s.gsub([regex_goes_here],'''') > > Now, s becomes: > > $: s > => "Here are some words. More words." > > I just don''t know the regular expression for this. Is it something > like... > > s.gsub(/^[:<pre>:][:</pre>:]$, '''') > > ??? > > That doesn''t seem to work. Any help on this? Thanks in advance. > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Shouldn''t the second argument be single quote followed by another single quote to remove the text between the tags as well as the tags? On 12/7/06, Jason Norris <jasonmnorris-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > gsub(/<pre>.*<\/pre>/, "<pre></pre>") > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bala Paranj wrote:> Shouldn''t the second argument be single quote followed by another single > quote to remove the text between the tags as well as the tags?That''s exactly what I was thinking. I have padding in the CSS on my "pre" tags, so if there isn''t any text there, I don''t want a block element showing. But that wasn''t the part I asked about - and it''s easy enough to just switch it out and see if that works. The hard part was the regex part, which I really appreciate. Regular expressions are always so easy once I see it, but can be complicated when I''m trying to write one. Thanks again for the help... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan, here is the Ruby mailing list: ruby-talk-google ruby-talk-X+L+6nJQZ58h9ZMKESR00Q@public.gmane.org http://groups.google.com/group/ruby-talk-google For Ruby related questions, you will find this group very helpful. On 12/7/06, ryan <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > > Thanks again for the help... > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
So, maybe I''m doing something wrong, but this isn''t working: gsub(/<pre>.*<\/pre>/, "<pre></pre>") Here''s what I have: #application_helper.rb def strip_pre_block(text) text.gsub(/<pre>.*<\/pre>/, '''') text end #view <%= strip_pre_block(to_html(p.body)) %> The to_html converts the textile text into html. See anything wrong with this regex? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
yes. You''re gsubbing the text, but not doing anything with it. Then you''re returning your original text. Take that last statement out of your method, then it will return the results of the gsub. ryan wrote:> So, maybe I''m doing something wrong, but this isn''t working: > > gsub(/<pre>.*<\/pre>/, "<pre></pre>") > > Here''s what I have: > > #application_helper.rb > def strip_pre_block(text) > text.gsub(/<pre>.*<\/pre>/, '''') > text > end > > #view > <%= strip_pre_block(to_html(p.body)) %> > > The to_html converts the textile text into html. > > See anything wrong with this regex? > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I don''t think anyone mentioned that the regex will not work if you have more than one <pre> block in the text. In other words, it will fail in this case: str = "<pre>DeleteMe</pre>SaveMe<pre>DeleteMe</pre>" Because the .* is greedy, it''ll get rid of the entire string, including the SaveMe part. If you KNOW there won''t be any HTML tags inbetween the <pre></pre> tags, you can do the following: def strip_pre_block(text) text.gsub(/<pre>[^<]*<\/pre>/,'''') end HTML is difficult to parse correctly using Regular Expressions. Ordinarily I would recommend using a real HTML parser, but in your case, you may be able to use the amended regex above, or even process the textile before you convert it to HTML. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thank you - the regex you gave me worked like a charm. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---