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.
------_ extPart_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" <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 ile.
> > > 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 ile.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
------_ extPart_001_01C0EDF9.42192E40
Content-Type: text/html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV
ontent-Type" CONTENT ext/html; charset -ASCII">
<META NAME enerator" CONTENT S Exchange Server version 5.5.2653.12">
<TITLE>RE: [ruby-talk:16248] Re: line numbers in multiline regular expressions.</TITLE>
</HEAD>
<BODY>
<P><FONT SIZE > -----Original Message-----</FONT>
<BR><FONT SIZE > From: jonas [<A HREF ailto:jonas.bulow / servicefactory.se">mailto:jonas.bulow / servicefactory.se</A>]</FONT>
<BR><FONT SIZE > Sent: Tuesday, June 05, 2001 3:36 PM</FONT>
<BR><FONT SIZE > To: ruby-talk / ruby-lang.org</FONT>
<BR><FONT SIZE > Subject: [ruby-talk:16248] Re: line numbers in multiline regular</FONT>
<BR><FONT SIZE > expressions.</FONT>
<BR><FONT SIZE > </FONT>
<BR><FONT SIZE > </FONT>
<BR><FONT SIZE > Wayne Blair wrote:</FONT>
<BR><FONT SIZE > > </FONT>
<BR><FONT SIZE > > ----- Original Message -----</FONT>
<BR><FONT SIZE > > From: "jonas" <jonas.bulow / servicefactory.se></FONT>
<BR><FONT SIZE > > To: "ruby-talk ML" <ruby-talk / ruby-lang.org>; </FONT>
<BR><FONT SIZE > <ruby-talk / netlab.co.jp></FONT>
<BR><FONT SIZE > > Sent: Tuesday, June 05, 2001 3:01 PM</FONT>
<BR><FONT SIZE > > Subject: [ruby-talk:16245] line numbers in multiline </FONT>
<BR><FONT SIZE > regular expressions.</FONT>
<BR><FONT SIZE > > </FONT>
<BR><FONT SIZE > > > Hi,</FONT>
<BR><FONT SIZE > > ></FONT>
<BR><FONT SIZE > > > Here is my 5 minute attempt to create a program that extracts all</FONT>
<BR><FONT SIZE > > > comments in a C source file containing the string "TODO":</FONT>
<BR><FONT SIZE > > ></FONT>
<BR><FONT SIZE > > > todo_comments ile.</FONT>
<BR><FONT SIZE > > > open(ARGV[0], "r").</FONT>
<BR><FONT SIZE > > > read.</FONT>
<BR><FONT SIZE > > > scan(/\/\*.*?\*\//m).</FONT>
<BR><FONT SIZE > > > delete_if { |c| c !~ /TODO/ }</FONT>
<BR><FONT SIZE > > ></FONT>
<BR><FONT SIZE > > > It works fine, but I really would like to have access to </FONT>
<BR><FONT SIZE > the line number</FONT>
<BR><FONT SIZE > > > where the scan matches. Is that easy to accomplish or do I have to</FONT>
<BR><FONT SIZE > > > rethink the structure of the program from scratch?</FONT>
<BR><FONT SIZE > > ></FONT>
<BR><FONT SIZE > > > /j</FONT>
<BR><FONT SIZE > > ></FONT>
<BR><FONT SIZE > > The scan can match across multiple lines, though, right? </FONT>
<BR><FONT SIZE > Can you give an</FONT>
<BR><FONT SIZE > > example of input and desired output?</FONT>
<BR><FONT SIZE > </FONT>
<BR><FONT SIZE > </FONT>
<BR><FONT SIZE > Input:</FONT>
<BR><FONT SIZE > </FONT>
<BR><FONT SIZE > ------------------------8<-------------</FONT>
<BR><FONT SIZE > /* comment 1 */</FONT>
<BR><FONT SIZE > </FONT>
<BR><FONT SIZE > /*</FONT>
<BR><FONT SIZE > * comment 2</FONT>
<BR><FONT SIZE > */</FONT>
<BR><FONT SIZE > </FONT>
<BR><FONT SIZE > /* </FONT>
<BR><FONT SIZE > * TODO foo bar</FONT>
<BR><FONT SIZE > * flopp flerp plopp plerp blipp</FONT>
<BR><FONT SIZE > * blopp boo.com</FONT>
<BR><FONT SIZE > */</FONT>
<BR><FONT SIZE > ------------------------8<-------------</FONT>
<BR><FONT SIZE > </FONT>
<BR><FONT SIZE > </FONT>
<BR><FONT SIZE > </FONT>
<BR><FONT SIZE > Output: </FONT>
<BR><FONT SIZE > </FONT>
<BR><FONT SIZE > I would like the line number of the line where "TODO" match </FONT>
<BR><FONT SIZE > and it's the</FONT>
<BR><FONT SIZE > comment's complete content, i.e the complete last comment above.</FONT>
<BR><FONT SIZE > </FONT>
</P>
<P><FONT SIZE What about IO.lineno? Here the documentation found at http://www.rubycentral.com/ref/ref_c_io.html#lineno:</FONT>
</P>
<P><FONT SIZE 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. </FONT></P>
<P><FONT SIZE f ile.new("testfile") </FONT>
<BR><FONT SIZE f.lineno -> 0 </FONT>
<BR><FONT SIZE f.gets -> "This is line one\n" </FONT>
<BR><FONT SIZE f.lineno -> 1 </FONT>
<BR><FONT SIZE f.gets -> "This is line two\n" </FONT>
<BR><FONT SIZE f.lineno -> 2 </FONT>
</P>
<P><FONT SIZE I guess you could easily adapt your code above to use it.</FONT>
</P>
<P><FONT SIZE Christophe</FONT>
</P>
</BODY>
</HTML>
------_ extPart_001_01C0EDF9.42192E40--