From: Ezra Zygmuntowicz Date: 2005-09-15T03:17:28+09:00 Subject: Re: Quick and dirty word wrapping. On Sep 14, 2005, at 7:59 AM, James Edward Gray II wrote: > On Sep 14, 2005, at 9:48 AM, Gavin Kistner wrote: > > >> On Sep 14, 2005, at 8:38 AM, James Edward Gray II wrote: >> >> >>>> str.scan(/(.{1,30})(?:\s+|$)/) >>>> >>>> >>> I'm still puzzling out how that works... >>> >>> >> >> Up to 30 characters, but there has to be whitespace after it (to >> keep it from splitting in the middle of the word) or be the very >> end of the string. The greedy regexp will grab all 30 if it can >> find them with whitespace after, otherwise it will backtrack until >> it finds the right spot. >> > > I do understand the Regexp, but isn't that a look-ahead assertion > at the end? That's not supposed to consume characters, right? So > why doesn't the very next match start with the leading whitespace > that ended the last match? > > I know I just haven't got me head all the way around it yet. I'm > working on it... ;) > > James Edward Gray II > > I have been using something extremely similar to add some tags to a text dump of the classified ads for my newspaper. Like this: line = "Clinician PT front office, 50-60 hrs/mo. Send resumes to: www.omacime.com" line.gsub!(/(]+>)(.{1,50}.*?\b)/, "\\1\\2<\/ftditm>") #=> "Clinician PT front office, 50-60 hrs/mo. Send resumes to: www.omacime.com" This takes a line and wraps the tags around 50 chars plus whatever is needed to make it to a whitespace char. Cheers- -Ezra Zygmuntowicz Yakima Herald-Republic WebMaster 509-577-7732 ezra@yakima-herald.com