aspose file tools
The moose likes Java in General and the fly likes Line number in a source code Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Line number in a source code" Watch "Line number in a source code" New topic
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Line number in a source code
 
Similar Threads
[newbie] regex anomaly
Need help to parse text files.
Stack Trace
Regular Expressions on a .csv file
how to retrive more than one match