Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Finding row number for a Java regexp pattern match

Thread view: 
ken1@tjohoo.se - 07 Feb 2006 15:12 GMT
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?

Kenneth Ljunggren
Gordon Beaton - 07 Feb 2006 15:21 GMT
> 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
[quoted text clipped - 3 lines]
> 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

Signature

[  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

Roedy Green - 08 Feb 2006 23:24 GMT
>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
[quoted text clipped - 3 lines]
>Is there some way to ontain the row number taken into consideration
>both Unix and windows newlines?

You could read by lines yourself and feed the lines one at a time to
your pattern matcher. Then you would know.

I don't think it will even tell you the offset, so there is no point
in creating a map of offsets to line numbers.

You might do some crude pattern matching yourself on a giant string
representing the entire file to find candidates where you count line
endings as you go, the feed them to regex for confirmation.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.