From: Robert Klemme Date: 2005-09-14T22:51:33+09:00 Subject: Re: Surprising Regexp Behavior James Edward Gray II wrote: > On Sep 14, 2005, at 8:21 AM, Robert Klemme wrote: > >> Ah! In that case I'd something like: >> >> html.sub!(/(

)(.*?)(<\/p>)(.*)/) { $1 << $2.strip << $3 } >> html.sub!(/

(.*?)<\/p>(.*)/) { "

#{$1.strip}

" } > > The method takes a chunk of HTML and pulls the first paragraph out of > it (minus the

and

tags). But I want to know if there was > other content, so I can add an ellipses if needed. > > Here's the entire method, defined in a Rails helper module: > > def excerpt( textile, id ) > html = sanitize(textilize(textile)) > html.sub!(/

(.*?)<\/p>(.*)\Z/m) { $1.strip } > if $2 =~ /\S/ > "#{html} #{link_to '...', :action => :show, :id => id}" > else > html > end > end > > It works as expected now. This might be a bit more efficient (dunno how often you call it): def excerpt( textile, id ) html = sanitize(textilize(textile)) html.sub!(/

(.*?)<\/p>(.*)\Z/m) { $1.strip } html << link_to( '...', :action => :show, :id => id ) if $2 =~ /\S/ html end An alternative def excerpt( textile, id ) html = sanitize(textilize(textile)) html.sub!(/

(.*?)<\/p>.*(\S)?\Z/m) { $1.strip } html << link_to( '...', :action => :show, :id => id ) if $2 html end Just an idea... Cheers robert