From: Christophe Broult Date: 2001-06-06T04:52:59+09:00 Subject: [ruby-talk:16249] Re: line numbers in multiline regular expre ssions. This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. ------_=_NextPart_001_01C0EDF9.42192E40 Content-Type: text/plain > -----Original Message----- > From: jonas [mailto:jonas.bulow@servicefactory.se] > Sent: Tuesday, June 05, 2001 3:36 PM > To: ruby-talk@ruby-lang.org > Subject: [ruby-talk:16248] Re: line numbers in multiline regular > expressions. > > > Wayne Blair wrote: > > > > ----- Original Message ----- > > From: "jonas" > > To: "ruby-talk ML" ; > > > 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. > What about IO.lineno? Here the documentation found at http://www.rubycentral.com/ref/ref_c_io.html#lineno: Returns the current line number in ios. The stream must be opened for reading. lineno counts the number of times gets is called, rather than the number of newlines encountered. The two values will differ if gets is called with a separator other than newline. See also the $. variable. f = File.new("testfile") f.lineno -> 0 f.gets -> "This is line one\n" f.lineno -> 1 f.gets -> "This is line two\n" f.lineno -> 2 I guess you could easily adapt your code above to use it. Christophe ------_=_NextPart_001_01C0EDF9.42192E40 Content-Type: text/html RE: [ruby-talk:16248] Re: line numbers in multiline regular expressions.

> -----Original Message-----
> From: jonas [mailto:jonas.bulow@servicefactory.se]
> Sent: Tuesday, June 05, 2001 3:36 PM
> To: ruby-talk@ruby-lang.org
> Subject: [ruby-talk:16248] Re: line numbers in multiline regular
> expressions.
>
>
> 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.
>

What about IO.lineno? Here the documentation found at http://www.rubycentral.com/ref/ref_c_io.html#lineno:

Returns the current line number in ios. The stream must be opened for reading. lineno counts the number of times gets is called, rather than the number of newlines encountered. The two values will differ if gets is called with a separator other than newline. See also the $. variable.

f = File.new("testfile")
f.lineno -> 0
f.gets -> "This is line one\n"
f.lineno -> 1
f.gets -> "This is line two\n"
f.lineno -> 2

I guess you could easily adapt your code above to use it.

Christophe

------_=_NextPart_001_01C0EDF9.42192E40--