| Author |
Line number in a source code
|
Lucky J Verma
Ranch Hand
Joined: Apr 11, 2007
Posts: 277
|
|
I am writing a snippet program picking out a snippet from a source and i use regex to find the term(location of snippet)
Probelm is : i also want to add line numbers ,where that snippet is lying in source code.So while displaying snippet i want to attach linenums.
Is there any way using regex,where i can get lines number too where the match is found?
My regex looks for first appearance of term(some search term) in code.
|
 |
Vinoth Kumar Kannan
Ranch Hand
Joined: Aug 19, 2009
Posts: 276
|
|
Is there any way using regex,where i can get lines number too where the match is found?
I dont think so..
According to the regex engine, you give it a set of characters(or a String simply) - 'blah blah blah \n blah blah blah \n....'. It'll just help you match your pattern in the input string, but will not keep track of the line number('\n') and it is not designed to do so... but, you can always get the start/end index where your match occurred and do some work around with it to find the line number.
|
OCPJP 6
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
One solution is to use a LineNumberReader around a StringReader around the String, then use the regex matching on each separate line. The LineNumberReader can give you the current line number (0-based of course).
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: Line number in a source code
|
|
|