Finding row number for a Java regexp pattern match

Discussion in 'Java' started by ken1@tjohoo.se, Feb 7, 2006.

  1. Guest

    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
     
    , Feb 7, 2006
    #1
    1. Advertising

  2. 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
     
    Gordon Beaton, Feb 7, 2006
    #2
    1. Advertising

  3. Roedy Green Guest

    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.
     
    Roedy Green, Feb 8, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mikel Lindsaar
    Replies:
    0
    Views:
    523
    Mikel Lindsaar
    Mar 31, 2008
  2. Old Echo
    Replies:
    1
    Views:
    193
    Adam Shelly
    Sep 4, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    390
    7stud --
    Aug 21, 2009
  4. Eric

    RegExp to match pattern or BLANK?

    Eric, Dec 17, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    96
    David Oswald
    Dec 17, 2003
  5. Uldis  Bojars
    Replies:
    2
    Views:
    204
    Janwillem Borleffs
    Dec 17, 2006
Loading...

Share This Page