Regular expression help: unable to search ' # ' character in the file

dudeja.rajat at gmail.com dudeja.rajat at gmail.com
Sat Sep 27 08:29:34 EDT 2008


Hi,

Can some help me with the regular expression. I'm looking to search #
character in my file?

My file has contents:

###############################

Hello World

###############################

length = 10
breadth = 20
height = 30

###############################



###############################

Hello World

###############################

length = 20
breadth = 30
height = 40

###############################


I used the following search :

import re

fd = open(file, 'r')
line = fd.readline
pat1 = re.compile("\#*")
            while(line):
                mat1 = pat1.search(line)
                if mat1:
                    print line
                    line = fd.readline()


But the above prints the whole file instead of the hash lines only.


Please help


Regards,
Rajat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080927/54c8a11f/attachment.html>


More information about the Python-list mailing list