All, I apologize that this is really a Ruby question, but it is bringing my rails app to a standstill and I need some help. Basically, I have some marked-up text (in files) that I have to massage into the database. The FileIO and DB stuff is easy. However, I also need to make some changes to the original text as it gets entered. This is best illustrated by an example. Original: This is my sample text. It is a paragraph about <n>nothing at all</n>. Thanks! Desired Output: This is my sample text. It is a paragraph about <span id="keyword_nothing_at_all">nothing at all</span>. Thanks! So far, I''ve got this for my code: expression = /<n>(.*?)<\/n>/ replacement_tag = ''<span id="keyword_\1">\1</span>'' paragraph.gsub!(expression, replacement_tag) This works great for creating the span tags, but I need to modify the \1 that appears in the id=" " portion by replacing any non-letter characters (like <>:''_;"\s?/\) with an underscore. I''ve been banging my head on the pickaxe for an hour and can''t figure out how to do this. Help, please? Thanks, Jeff
Jeff Casimir wrote:> Original: > This is my sample text. It is a paragraph about <n>nothing at all</n>. > Thanks! > > Desired Output: > This is my sample text. It is a paragraph about <span > id="keyword_nothing_at_all">nothing at all</span>. Thanks! > > So far, I''ve got this for my code: > expression = /<n>(.*?)<\/n>/ > replacement_tag = ''<span id="keyword_\1">\1</span>'' > paragraph.gsub!(expression, replacement_tag) > > This works great for creating the span tags, but I need to modify the \1 > that appears in the id=" " portion by replacing any non-letter > characters (like <>:''_;"\s?/\) with an underscore. I''ve been banging my > head on the pickaxe for an hour and can''t figure out how to do this.Try this (untested): paragraph.gsub!(/<n>(.*?)<\/n>/) do |match| body = $1 id = body.tr("<>:''_;"\s?/\\", "_") %{<span id="keyword_#{id}">#{body}</span>} end
Worked with a little tweak to escape a double-quote in the tr() command. Thank you!!! -Jeff> > Try this (untested): > > paragraph.gsub!(/<n>(.*?)<\/n>/) do |match| > body = $1 > id = body.tr("<>:''_;"\s?/\\", "_") > %{<span id="keyword_#{id}">#{body}</span>} > end > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails