When i search for a pattern in a file how do i get the line and line#? basically i want to replicate the 'grep' command functionality. Now i can easily say whether the pattern exists or not using re.search funtion but i'm not able to print that particular line and line#. are there any functions in re module for that?

Maybe you need to do a line by line match in a loop and then line/line number shouldn't be a problem. I'm not sure how much slower it may be compared to what you're doing now but you can e.g. download a big plain-text e-book and do some tests.



There are several ways you could do this, however, this is one way.

import re

def main():
    file = open('your_file_here.txt')
    lines = file.readlines()

    pattern = re.compile('&(?!#)(?!amp;)(?!quot;)(?!lt;)(?!gt;)')
    for i, line in enumerate(lines):
        if pattern.search(line):
            print i, line

if __name__ == '__main__':

In the above I am searching the "your_file_here.txt" file for just an ampersand "&" all by itself. The code will print out the line number and the line that was matched to the pattern.

Trust that helps.


[ http://www.platoscave.net ]

worked like a charm... thanks a lot Wayne ... i didnt know about this 'readlines' function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.