Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Finding Line Number When Matching Text

DZone's Guide to

Finding Line Number When Matching Text

·
Free Resource
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)

src = open('2.htm').read()
pattern = '

([^<]+)' # or anything else for m in re.finditer(pattern, src): start = m.start() lineno = src.count('\n', 0, start) + 1 offset = start - src.rfind('\n', 0, start) word = m.group(1) 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.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}