• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Line number in a source code

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
reply
    Bookmark Topic Watch Topic
  • New Topic