On Tue, Jun 05, 2001 at 09:36:00PM +0100, ruby-talk / ruby-lang.org wrote: > Wayne Blair wrote: > > > > ----- Original Message ----- > > From: "jonas" <jonas.bulow / servicefactory.se> > > To: "ruby-talk ML" <ruby-talk / ruby-lang.org>; <ruby-talk / netlab.co.jp> > > Sent: Tuesday, June 05, 2001 3:01 PM > > Subject: [ruby-talk:16245] line numbers in multiline regular expressions. > > > > > Hi, > > > > > > Here is my 5 minute attempt to create a program that extracts all > > > comments in a C source file containing the string "TODO": > > > > > > todo_comments = File. > > > open(ARGV[0], "r"). > > > read. > > > scan(/\/\*.*?\*\//m). > > > delete_if { |c| c !~ /TODO/ } > > > > > > It works fine, but I really would like to have access to the line number > > > where the scan matches. Is that easy to accomplish or do I have to > > > rethink the structure of the program from scratch? > > > > > > /j > > > > > The scan can match across multiple lines, though, right? Can you give an > > example of input and desired output? > > > Input: > > ------------------------8<------------- > /* comment 1 */ > > /* > * comment 2 > */ > > /* > * TODO foo bar > * flopp flerp plopp plerp blipp > * blopp boo.com > */ > ------------------------8<------------- > > > > Output: > > I would like the line number of the line where "TODO" match and it's the > comment's complete content, i.e the complete last comment above. You could try something like this --- maybe a bit non-Rubyesque, but it'll work line_nr = 0 begin_ln = 0 while file.gets line_nr+=1 if comment comment <<= $_ if %r{\*/} print begin_ln, line_nr, comment if comment =~ /TODO/ comment = nil end elsif %r{/\*} comment = $_.dup begin_ln = line_nr end end Renald