On 7 Feb 2006 07:12:38 -0800,
wrote:
> I am using the Java regexp package classes Pattern and Matcher to
> find matches in alot of text files i want to process to generate a
> list of matches per file and which line numbers they where find on.
> But I haven't found any easy way of finding out the line number on
> which the match was found using the standard regexp classes. Is
> there some way to ontain the row number taken into consideration
> both Unix and windows newlines?
The regexp classes are not an appropriate tool for counting lines.
If you are reading your text files with BufferedReader.readLine(),
then the most suitable way to determine what line you are on is to
simply keep track of it in a counter. Reset the counter to zero when
you open the file, and increment it after reading each line.
BufferedReader.readLine() handles both of the line ending styles you
mention.
I get the impression from your question that you are reading in the
entire file before doing your matches. Unless you are looking for
patterns that span more than one line, I'd suggest that your program
would be simpler if you handled each line separately, i.e. read one
line, do the necessary matching, then read the next line. Your line
counting issue will solve itself.
/gordon
--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e