Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Finding row number for a Java regexp pattern match

Reply
Thread Tools

Finding row number for a Java regexp pattern match

 
 
ken1@tjohoo.se
Guest
Posts: n/a
 
      02-07-2006

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

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      02-07-2006
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
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      02-08-2006
On 7 Feb 2006 07:12:38 -0800, wrote, quoted or
indirectly quoted someone who said :

>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?


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.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
String#match vs. Regexp#match - confused Old Echo Ruby 1 09-04-2008 06:11 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:50 PM
RegExp to match pattern or BLANK? Eric Perl Misc 2 12-17-2003 05:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57