Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Korakot Chaovavanich http://korakot.stumbleupon.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Finding line number when matching text

I use python to do some text analysis.
I load all the text file into a string and match it with
a regexp. Now I want to know the line number of a match.
I can use the line number to lookup inside the text.
(in this case using EditPlus)
   1  
   2  src = open('2.htm').read()
   3  pattern = '<P>([^<]+)<SUP>'  # or anything else
   4  for m in re.finditer(pattern, src):
   5  	start = m.start()
   6  	lineno = src.count('\n', 0, start) + 1
   7  	offset = start - src.rfind('\n', 0, start)
   8  	word = m.group(1)
   9  	print "2.htm(%s,%s): %s" % (lineno, offset, word)

Editplus allow me to double click at the output printed
and jump to the exact position using the given lineno, offset.
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS