----- 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 > Here's one hacked up idea: # first load file with line numbers # into a temp buffer then parse that file=File.open(ARGV[0], "r") inputWithLineNumbers = "" currentLineNumber = 0; file.each do |line| currentLineNumber += 1 inputWithLineNumbers += "line " + currentLineNumber.to_s + line + "\n" end p inputWithLineNumbers.scan(/line[^\n]*?\/\*.*?\*\//m).delete_if { |c| c !~ /TODO/ } Wayne